当我尝试创建对象数组时出错 [英] Error when I try to create array of object

查看:66
本文介绍了当我尝试创建对象数组时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑,对我来说,以下两种实现是有效的(但是我的语法有错误),但是我不能使它们中的任何一个都起作用.这是一年级的C ++入门类功课,我们不能使用Vector(否则将在很久之前完成...)

I am getting quite a bit confused, to me I think the following two implementation are valid (but my syntax has error), but I can not get either one of them to work. This is a year 1 C++ intro class homework, we can not use Vector (or else it will be done a long time ago...)

我已经完成我的课程 Student ,但是现在我必须创建一个容器来存储n个学生.我在想以下两种选择.

I already finished my class Student, but now I have to create a container to store n students. I was thinking the following two option.

1)创建一个学生对象数组: Student arrayOfStudents [20];

1) Create an array of student objects: Student arrayOfStudents[20];

我的学生班很简单(由于行数过多,修改后有所不同).但是这个主意是这样的...

My student class is very simple (It is modified different because too many lines). But the idea is like this...

class Student
{
    public:
    // Constructor for the Book class, take 6 arguments 
    Student(string myUID, string myLastName, string myFirstName, 
         string major, string age, string homeState, bool isWorking);
    ...
};

但是当我尝试创建一系列学生时,会收到此错误消息.

But when I try to create an array of students, I get this error message.

cs114p2main.cpp: In function ‘int main(int, char**)’:
cs114p2main.cpp:103: error: no matching function for call to ‘Student::Student()’
cs114p2Student.h:14: note: candidates are: Student::Student(std::string*)
cs114p2Student.h:11: note:                 Student::Student(std::string, std::string, std::string, std::string, std::string, bool)
cs114p2Student.h:7: note:                 Student::Student(const Student&)

2)也尝试过:创建指向每个学生的学生指针数组. Student * ArrayOfStudents = Student [20];

2) Also tried: Create an array of student pointer that points to each student. Student* ArrayOfStudents = Student[20];

我的问题是方法1为什么不起作用?在这种情况下,我是否必须使用指针(方法2的方法).

My question is why doesn't the method 1 work? And do I have to use pointers in this case (something along the line of method 2).

因为我需要将此数组传递给要设置每个学生的函数.(我不知道会有多少学生在那里).那是否意味着我必须将指针传递给学生数组?在那种情况下,我真的希望方法1可以工作,我无法想象处理指向学生指针数组的指针.我当时在想必须返回一个 data [numOfData] [numOfStudnet] 数组,并主要执行此操作,但是无论哪种方式,我都想弄清楚为什么这会给我带来错误..谢谢.

Because I need to pass this array into a function that going to set up each student. (I do not know how many students are going to be there). So does that mean I have to pass a pointer to the array of students? In that case I really hope method 1 can work, I can't imagine dealing with pointer to array of student pointers. I was thinking have to return an array of data[numOfData][numOfStudnet], and do it in the main, but either way I would like to figure out why this gives me error.. thank you.

最常见的问题:我通过研究知道,我必须声明 Student 数组的大小作为参数,但是如果我只知道大小,该怎么办?它运行后?在那种情况下,我有什么选择?

Follwo up question : I know through research, I must declare the size of the array of Student as the parameter, but what if I only know the size after it is running? In that case, what are my options?

推荐答案

您的 Student 类只有一个构造函数,即

Your Student class has only one constructor, namely

Student(string myUID, string myLastName, string myFirstName,  
     string major, string age, string homeState, bool isWorking); 

因此,编译器不会生成默认构造函数.由于您也不提供任何参数(默认ctor:一个可调用而没有任何参数),因此编译器无法创建数组-它需要将默认可构造对象放置在元素中(使用初始化数组).

Because of this, the default constructor does not get generated by the compiler. Since you also don't provide one (default ctor: one callable with no arguments), the compiler cannot create the array -- it would need default-constructable objects to place in the elements (initialize the array with).

要修复,您需要提供默认的ctor:

To fix, you will need to provide a default ctor:

Student();

您需要根据假定的学生的行为来找出自己的行为.

the behavior of which you will need to find out yourself based on how Student is assumed to behave.

您还可以为列出的 Student ctor的所有参数提供默认参数,因此可以在不显式指定任何参数的情况下调用该默认参数-因此有资格作为默认ctor

You could also provide default arguments to all parameters of the listed Student ctor, so it is callable without explicitly specifying any of the parameters -- thus qualifies as a default ctor

如果默认ctor不可行,则需要变得更有创造力并动态分配数组,将所有现有元素复制到新元素,并在需要时为其分配给定的新元素.请注意,这样做实际上是在重新创建 std :: vector 容器的功能.还要注意,这种方法真的很难正确解决,因此我建议您不要这样做,尤其是因为您只是在学习基础知识.

If a default ctor is not feasable, you will need to become more creative and dynamically allocate the array, copy all existing elements to the new one and assign the given new element to it when needed. Note that with this you are essentially re-creating the functionality of the std::vector container. Note also, that this approach is really hard to get it right, so I advise against it, especially since you are just learning the basics.

另一种方法是创建指向 Student 的指针的数组,因此在动态创建它们时可以分配它们.在尝试访问所指向的 Student 对象之前,您必须测试该元素是否为空指针.

Yet another approach is to create an array of pointers to Student, so you can assign each as you create them dynamically. You will have to test whether the element is a null pointer, before you attempt to access the Student object pointed to.

Student* students[20];

students[0] = new Student(/*list of actual parameter values*/);

if (student[0] != NULL) {
  students[0]->getName(); // assuming you have a member function getName()
}

注意:索引0仅用于说明目的,也可以使用变量(只要确保它在0到19(含)之内-注意,即使您有20个元素,最大的索引也要注意是19,因为从0开始计数

Note: the index 0 is used only for illustrative purposes, you can use a variable as well (as long as you ensure it stays within 0 and 19 (inclusive) -- note even though you have 20 elements, the largest index is 19 as the counting starts from 0

这篇关于当我尝试创建对象数组时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆