[英]Getting Error when trying to pass an array of object to a function
我正在尝试通过一系列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函数,以便在读取文件时可以设置每个数组。
错误消息告诉您答案:
学生*”到“学生**”
在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
第一个错误(自从我写这篇文章以来,似乎已从问题中删除了它)是告诉您processStudent
的第二个参数是指针到指针Student**
,而不是指针Student*
。 这很奇怪,因为您显示带有Student*
参数的声明; 您确定这是代码中的实际声明吗? 您在某处有其他声明吗?
第二个错误可能是因为您没有链接到包含构造函数定义的单元。 您仅用一个源文件( csci135p2main.cpp
)调用编译器,而我猜您在另一个源文件中定义了构造函数。
您应该问自己一些可以帮助您的问题:
“如何创建学生的新实例?”
好吧,我这样做是这样的: 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
而不是array,使用对象而不是指针,然后使用聪明的指针,而不是至少裸露的指针。
您最好使用Student的向量(最好仅使用具有基本类型(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
类实现移动语义 ,或vector<shared_ptr<Student>>
或vector<unique_ptr<Student>>
根据您的所有权(共享与否) Student
实例。
如果学生数组是输入/输出参数,则您的函数原型应该是这样的:
void processStudent(const std::string & filename, std::vector<Student> & students);
相反,如果processStudent
函数创建学生数组(例如,基于文件的内容),则仅将其作为返回值返回(由于移动语义,这非常有效):
std::vector<Student> processStudent(const std::string & filename);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.