尝试将对象数组传递给函数时出错

Getting Error when trying to pass an array of object to a function

本文关键字:函数 出错 对象 数组      更新时间:2023-10-16

我正在尝试传递一个Student数组

进入函数processStudent(string myFilename, Student* myArray, int &mySize)。 但它给了我不同类型的错误。

Student() 什么都不做,但我试图为它们分配某种值,它仍然给出完全相同的错误消息:

总的来说,我有这个:

// Create an array of students, with the size of theMax (256)
Student* arrayOfStudent= new Student[theMax];
// An integer that will keep track of actually how many students
// Because when we loop, we want to loop within the cell
// that actually have data or student.
int actualSize = 0;
// Invoke the helper function to set up the array of students
// It passed the arryOfStudent by reference, so the changes
// inside of the function will be reflected when it returns
processStudent(filename, arrayOfStudent, actualSize);

函数是这样的:

void processStudent(string myFilename, Student* myArray, int& mySize)
{
    // Something here, but removed still gives that error
}

在课堂上 学生的 cpp 文件

Student::Student() 
{
    // Nothing here
}

错误信息:

new-host-2:csci135p1 george$ g++ -Wall -o csci135p2main csci135p2main.cpp
Undefined symbols for architecture x86_64:
  "Student::Student()", referenced from:
      _main in cc3fTXti.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

我一直在剥离和剥离我的代码,但此错误不会消失。我想创建这个数组,并将其传递给processStudent函数,以便它可以在读取文件时设置每个数组。

你应该问自己一些可以帮助你的问题:

"如何创建学生的新实例?"
好吧,我是这样做的:Student* s = new Student(); - 它创建新对象并将对它的引用存储为指针 ( Student*

"那么如何创建一个 10 Student 秒的数组呢?"
好吧,它可能会是一系列指向新Student的指针,我可能不得不不止一次地打电话给new......通过这样思考,你很容易得到这样的东西:

Student** students = new Student*[10];
for (int i = 0; i < 10; ++i)
    students[i] = new Student();

。这意味着,当你清理它时,你将不得不在每个Student*上调用delete,而且你必须清理数组本身:在Student**上调用delete[] - 当你意识到丑陋的内存管理连接与指针数组连接时,它应该让你寻找更简单更好的实现方法, 因此,如果可能的话,您最终应该使用std::vector而不是数组,对象而不是指针,如果可能的话,则至少使用智能指针而不是裸指针。

第一个

错误(自从我写这篇文章以来似乎已经从问题中删除了)告诉你processStudent的第二个参数是一个指针到指针,Student**,而不是指针,Student*。这很奇怪,因为您使用Student*参数显示声明;您确定这是代码中的实际声明吗?你在某处有不同的声明吗?

第二个错误可能是因为您没有与包含构造函数定义的单元链接。您只使用一个源文件 ( csci135p2main.cpp ) 调用编译器,我猜您在不同的源文件中定义构造函数。

有错误消息告诉您答案:

学生

*"到"学生**"

processStudent() 中更改 myArray 参数的类型:

void processStudent(string myFilename, Student** myArray, int& mySize)
{
}

当一个数组被传递给一个函数时,它会衰减到一个指针:

void some_func(char* b) {...}
char buf[100];
some_func(buf);

当您传递arrayOfStudent数组衰减为指针时,恰好该数组是一个指针数组,因此** .


链接器抱怨它找不到 Student 的默认构造函数的定义。定义包含在Student.cpp文件中,因此请编译所有源文件以解决链接器错误:

g++ -Wall -o csci135p2main csci135p2main.cpp Student.cpp

你最好使用学生向量(更喜欢只使用基本类型(int,char..)的数组和其他所有向量)。

#include <vector>
std::vector<Student*> arrayOfStudent = new vector<Student*>();
/* ... */
processStudent(filename, arrayOfStudent, actualSize);

void processStudent(std::string& filename, vector<Student*>& arrayOfStudent, int& actualSize) {
/* ... */
}

我不确定这是否会解决您的问题,但至少这是一个更好的用途......

您的代码容易出错,难以维护异常,不安全C++。在现代C++中,您应该使用 std::vector<Student>Student类实现移动语义,或者基于Student实例的所有权(共享或非共享)vector<shared_ptr<Student>>vector<unique_ptr<Student>>

如果学生数组是输入/输出参数,您的函数原型应该是这样的:

void processStudent(const std::string & filename, std::vector<Student> & students);

相反,如果 processStudent 函数创建学生数组(例如,基于文件的内容),只需将其作为返回值返回(由于移动语义,这非常有效):

std::vector<Student> processStudent(const std::string & filename);