searching a vector of objects

Pages: 12
it does write the student names just the reg number thats 0

0 Mike Sanderson
reg = 0
0 Bart Simpson
reg = 0
0 Lisa Simpson
reg = 0
0 Homer Simpson
reg = 0
0 John Terry
reg = 0
0 Wayne Rooney
reg = 0
0 Cristiano Ronaldo
reg = 0
0 Lionel Messi
reg = 0
0 Tom Cruise
reg = 0
0 Monty Python


I plead not guilty! That is not the form of output I used in writeStudents(), so it's not from my code.


I plead not guilty! That is not the form of output I used in writeStudents(), so it's not from my code.

lol i just copied your write students again and i got similar output:

1
2
3
4
5
6
7
8
9
10
8199856  Mike Sanderson
reg = 8199856
8199856  Bart Simpson
reg = 8199856
8199856  Lisa Simpson
reg = 8199856
8199856  Homer Simpson
reg = 8199856
8199856  John Terry
reg = 8199856


Your constructor for Student is incomplete. It needs to set the regNo data member as well. Compare mine:
Student(const string &name, int regNo) : name( name ), regNo( regNo ) {}

You pass regNo correctly, but you don't use it.

Note that, since you are inheriting regNo from another class, you may have to use the constructor for that. Whatever you do ... regNo needs to be set for each Student object.

Last edited on
Your constructor for Student is incomplete. It needs to set the regNo data member as well. Compare mine:
Student(const string &name, int regNo) : name( name ), regNo( regNo ) {}


i initialised the name using :

1
2
        this->name = name;
        ;

added regNo to the initialisation list

but im getting error :

"student::regNo is private within this context" same for st.name


for ( Student &st : studentlists ) cout << st.regNo << " " << st.name << '\n';

Last edited on
@Blendero, please refer to my code. Specifically, the writeStudents() routine. It is now using the "getters" for these private variables:
for ( Student &st : studentlists ) cout << st.getRegNo() << " " << st.getName() << '\n';
and not direct access to the private variables regNo and name themselves.

I have defined these getters within my class definition:
1
2
   int getRegNo() { return regNo; };
   string getName() { return name; };

You appear to have the first of them already; you will need to create the second (unless, in your implementation, it is already inherited from Person).


I stress: if they are private data members then you will have to write "getters" in a class definition to get their values.
Last edited on
@lastchance thank you very much you're a lifesaver xd, it is now working as required with the correct regno output, its been a long one i will now mark this as solved.

thanks again,
Registered users can post here. Sign in or register to post.
Pages: 12