Create a vector of class instances in C ++

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 the Student class, but I could not understand this situation, here is my problem:

first 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 number 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 number 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;
}

output "error":

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 attempt, everything looked fine, but when I tried the following two options, I got errors, I want me to be able to understand what I'm doing wrong.

Thanks.

+5
source share
6 answers

It:

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); 

++ 11 , :

vector<Student>  ver = { 0, 1, 2, 3, 4, 5};

++ 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;
}
+15

, for . , for . for main() ( N / main(), .
, , , Student array[5]; vector<Student> ver[N];, Student Student(), . Student; id - , , -1.

+2

№1:

vector <Student> ver[N] vector<Student> ver(N)

std::vector - , , , N ( ) .

№ 2:

Student ver[N];

, Default Constructor Student() N , . Student ver[5] = {1, 2, 3, 4, 5} .

, , "for" .

+1

. ""

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;

}
0

The solution to this question is to create an array of instances of the Student class as Student ver [N]. Then, using the mutator function, setID (int i) to assign elements from the given student identifier in array format (int idStudent [N] = {11, 32, 37, 4, 50};) for each instance of the perspective Student class: ver [ i] .setId (idStudent [i]) and a for loop to complete the job.

Finally, using the access function, ver [i] .getID () and a for loop to display information.

0
source

All Articles