i created a class its name is Student as follows:
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
};
then after i wanted to have a container ( say a vector ) its elements are instances of class Student, but i found myself not able to understand this situation , here is my issue:
first i run this code:
#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;
}
everything is fine and the output is :
Hello, This is a code to learn classes
9
now when i try these options:
option #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;
}
i got this output "error" :
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;
^
option #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;
}
the output "error" was :
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 )
^
In the first try everything was looking ok , but when i tried the two next options , i received errors , i wish that i can understand what wrong i am doing.
Thanks.
vector <Student> ver[N];this does not create a vector with N students. It creates an array with N vector<Student>. You want `vector<Student> ver(N);