OOP : creating objects into a vector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Student
{
    private:
    string name;
    string gender;

    public:

    Student(){};
    Student(string sName, string sGender, int count);

};

...

 void createStudent(int n,Student student)
   {
      ...
      student.push_back(Student(studentName, studentGender, count));
      ...
   }


int main()
{
   vector<Student> student;
   ...
   ...
   ...
  
}


Sorry for making my code short, because I'm doing separate compilation so this program will be so long.

I just extracted the main part for my problems here.
student.push_back(Student(studentName, studentGender, count))
is inside a non-class function called createStudent.

1.I wanted to create a vector to store the data of students(objects) inside the vector named student. How ever complication error occur:

error: 'class Student' has no member named 'push_back'

I understand what is the error but I wonder if I have syntax error on my code in line 25.

2. What is the difference between declaring the vector inside class private member and declaring it in main function?

3. Am I correct that functions is allowed inside OOP? I use functions because it seems hard to make it as method and it seems irrelevant
Last edited on
You can't define functions inside main(). Functions are either part of a Class or global, never part of another function.

Your best bet is to define your function globally and have it require a vector<Student> as parameter (by reference. For the love of all that is holy, by reference!).

1
2
3
4
5
6
void createStudent(int n,Student student, vector<Student> &myStudents)
   {
      ...
      myStudents.push_back(Student(studentName, studentGender, count));
      ...
   }

Then, you can call it in your main.

(Disclaimer: I have no idea whether this code will compile. There might be other problems. I'm not even sure you can use a constructor as an argument for push_back.)
oh, sorry for the mistake I have done during extraction, Corrected the code that I had given just now.
My function is in global in my original code, that's why this error occur.
Ah, that makes sense. Your using the vector<Student> "student" in a global function, but "student" is limited to the scope of main(). In other words: createStudent() has no idea what "student" is.

There are two fixes: define "student" globally (please don't hurt me, Disch!), or what I suggested before. Obviously, the latter is the cleanest solution.

[edit]

Ah I just realized that is what you tried to do. You're passing a Student object to the function, then creating a new student and trying it to push_back it to your Student object, which makes no sense. Replace the parameter "Student student" by "vector<Student> &student" and you'll be fine.

Last edited on
erm may I ask why it is pass by reference , not pass by value? Vector is a type of dynamic allocation method which is same as array, and array is pass by reference by default but not for vector ?
I tried vector<Student> student it compiles fine... (not sure about the outcome I should get @@)
Last edited on
oOh okay I get the point already.
Thanks for your help!!
Just in case you're not exactly sure:

Passing by value makes a copy of the argument (here: vector of Student objects). This means:
a) ...it's a very costly operation, because the entire vector (= N Student objects!) have to be copied.
b) ...when you make any changes to it, you make them to the copy, not the original object. Since the copy is destroyed at the end of the function scope, all changes are lost.

Passing by reference solves those problem [and creates no new ones in this situation!].
Topic archived. No new replies allowed.