我创建了一个类,其名称为Student,如下所示:
class Student
{
private:
unsigned int id; // the id of the student
public:
unsigned int get_id(){return id;};
void set_id(unsigned int value) {id = value;};
Student(unsigned int init_val) {id = init_val;}; // constructor
~Student() {}; // destructor
};
然后在我想要一个容器(比如一个向量)后,它的元素是Student类的实例,但是我发现自己无法理解这种情况,这是我的问题:
首先我运行此代码:
#include<iostream>
#include<vector>
using namespace std;
const unsigned int N = 5;
Student ver_list[2] = {7, 9};
int main()
{
cout<< "Hello, This is a code to learn classes"<< endl;
cout<< ver_list[1].get_id() << endl;
return 0;
}
一切都很好,输出是:
Hello, This is a code to learn classes
9
现在当我尝试这些选项时:
选项#1:
#include<iostream>
#include<vector>
using namespace std;
const unsigned int N = 5;
vector <Student> ver[N]; // Create vector with N elements
for(unsigned int i = 0; i < N; ++i )
ver[i].set_id(i);
int main()
{
cout<< "Hello, This is a code to learn classes"<< endl;
cout<< ver[1].get_id() << endl;
return 0;
}
我得到了这个输出“错误”:
test.cpp:26:3: error: expected unqualified-id before 'for'
for(unsigned int i = 0; i < N; ++i )
^
test.cpp:26:27: error: 'i' does not name a type
for(unsigned int i = 0; i < N; ++i )
^
test.cpp:26:34: error: expected unqualified-id before '++' token
for(unsigned int i = 0; i < N; ++i )
^
test.cpp: In function 'int main()':
test.cpp:43:15: error: 'class std::vector<Student>' has no member named 'get_id'
cout<< ver[1].get_id() << endl;
^
选项#2:
#include<iostream>
#include<vector>
using namespace std;
const unsigned int N = 5;
Student ver[N]; // Create one dimensional array with N elements
for(unsigned int i = 0; i < N; ++i )
ver[i].set_id(i);
int main()
{
cout<< "Hello, This is a code to learn classes"<< endl;
cout<< ver[1].get_id() << endl;
return 0;
}
输出“错误”是:
test.cpp:30:14: error: no matching function for call to 'Student::Student()'
Student ver[5];
^
test.cpp:30:14: note: candidates are:
test.cpp:14:2: note: Student::Student(unsigned int)
Student(unsigned int init_val) {id = init_val;}; // constructor
^
test.cpp:14:2: note: candidate expects 1 argument, 0 provided
test.cpp:7:7: note: Student::Student(const Student&)
class Student
^
test.cpp:7:7: note: candidate expects 1 argument, 0 provided
test.cpp:31:1: error: expected unqualified-id before 'for'
for(unsigned int i = 0; i < N; ++i )
^
test.cpp:31:25: error: 'i' does not name a type
for(unsigned int i = 0; i < N; ++i )
^
test.cpp:31:32: error: expected unqualified-id before '++' token
for(unsigned int i = 0; i < N; ++i )
^
在第一次尝试时,一切都看起来不错,但是当我尝试下两个选项时,我收到了错误,我希望我能理解我在做什么错误。
感谢。
答案 0 :(得分:14)
此:
vector <Student> ver[N];
创建一个N
元素数组。每个元素都是vector<Student>
。这不是你想要的。您可能正在尝试创建N
元素的向量。其语法是:
vector <Student> ver(N);
但是你不能使用它,因为你的类没有默认的构造函数。因此,您的下一个选择是初始化具有相同元素的所有对象。
vector <Student> ver(N, Student(0));
您还试图像这样创建一系列学生:
Student ver[N];
这不起作用。因为它尝试使用默认构造函数初始化数组中的每个元素。但是你的类没有默认的构造函数。所以这不会奏效。但这就是你的原始代码确实有效的原因:
Student ver_list[2] = {7, 9}; // Here you are using the constructor for your object.
// It uses the normal constructor you provided not the default one.
其他问题是您无法在函数(方法)之外运行代码 所以这不起作用:
for(unsigned int i = 0; i < N; ++i )
ver[i].set_id(i);
在C ++ 11中,您可以像数组一样初始化矢量:
vector<Student> ver = { 0, 1, 2, 3, 4, 5};
如果您没有C ++ 11或初始化更复杂。然后你需要写一个包装器。
class VecWrapper
{
public:
std::vector<Student> ver;
VecWrapper()
{
ver.reserve(N);
for(unsigned int i = 0; i < N; ++i )
ver.push_back(Student(i));
}
};
现在您可以将其放在全局范围内,它将自动初始化。
VecWrapper myData; // myData.vec initializaed before main entered.
int main()
{}
选项2:
#include<iostream>
#include<vector>
using namespace std;
const unsigned int N = 5;
// The following is not correct
// This creates an arrya of `N` elements each element is `vector <Student>`
//
// vector <Student> ver[N]; // Create vector with N elements
//
// The following lines are not allowed.
// All code has to be inside a function.
//
// for(unsigned int i = 0; i < N; ++i )
// ver[i].set_id(i);
// What you want is:
// I use the following because it is unclear if you have C++11 or not.
class VecWrapper
{
public:
std::vector<Student> vec;
VecWrapper()
{
vec.reserve(N);
for(unsigned int i = 0; i < N; ++i )
vec.push_back(Student(i));
}
};
VecWrapper myData; // myData.vec
int main()
{
cout<< "Hello, This is a code to learn classes"<< endl;
cout<< myData.vec[1].get_id() << endl;
return 0;
}
答案 1 :(得分:2)
主要问题是你试图在全局范围内执行for循环。定义和初始化函数外部的变量是可以接受的,但是使用for循环或赋值运算符则不行。将for循环放入main()(我建议你也将N和vector / student数组放入main()中,一切都应该有效。
此外,编译器抱怨,因为当您声明Student array[5];
或vector<Student> ver[N];
时,它正在寻找一个名为Student()的默认构造函数,它只为类设置默认值。你需要在Student课程中提供这个;将id设置为某个值,该值永远不会是实际的学生ID,例如-1。
答案 2 :(得分:1)
选项#1:
您应该将vector <Student> ver[N]
替换为vector<Student> ver(N)
std :: vector是一个类,它自己表示向量,你不应该创建一个向量数组,你应该只将N(向量大小)传递给它的构造函数。 请检查此link
选项#2:
Student ver[N];
不正确,因为默认构造函数Student()被调用了N次,但您还没有实现它。
因此,您必须使用数组initilizer Student ver[5] = {1, 2, 3, 4, 5}
或显式实现默认构造函数。
当然 - 必须在函数体内使用“for”循环。
答案 3 :(得分:0)
这实际上与矢量完全没有联系。 您只需将“for”语句移至主
即可答案 4 :(得分:0)
#include<iostream>
using namespace std;
class Student
{
private:
int id;
public:
// Mutator
void setId(int i){
id = i;
}
// Accessor
int getId()const{
return id;}
};
int main()
{
const unsigned int N = 5;
Student ver[N]; // Define instances as an array
// of the Student class
int idStudent[N] = { 11, 32, 37, 4, 50}; // Create
// one dimensional array with N elements of the
// student ID
for(unsigned int i = 0; i < N; i++ ){ // Assign
//student ID for each object of the class
ver[i].setId(idStudent[i]);}
cout<< "Hello, This is a code to learn classes : "
<< endl << endl; // Display the student ID
for(unsigned int i = 0; i < N; i++ ){
cout<< "Student ID #" << i+1 << " of "
<< N << " : " << ver[i].getId() << endl;}
return 0;
}
答案 5 :(得分:0)
所提出问题的解决方案是将Student类的实例数组创建为Student ver [N]。接下来,使用mutator函数,使用setID(int i)将给定学生ID中的元素以数组格式(int idStudent [N] = {11,32,37,4,50};)分配给该对象的每个透视图实例。学生班:ver [i] .setId(idStudent [i])和for循环以完成作业。
最后,使用访问器函数ver [i] .getID()和for循环来显示信息。