What I'm trying to do here is to add Voter
to a vector
that belongs to RegLogBook
and, RegLogBook
is an object that is owned by my Election
class. I don't know what I'm doing wrong here, the Voter
object is not being added to the vector
. The relevant codes are below, I've removed all the unnecessary parts.
Election.h
class Election
{
public:
Election();
~Election();
RegLogBook getRegLogBook();
private:
RegLogBook* _regLogBook;
};
Election.cpp
Election::Election()
{
_regLogBook = new RegLogBook();
}
RegLogBook Election::getRegLogBook() {
return *_regLogBook;
}
RegLogBook.h
class RegLogBook
{
public:
RegLogBook();
~RegLogBook();
vector<Voter*> getVoterList();
private:
vector<Voter*> _voterList;
};
RegLogBook.cpp
vector<Voter*> RegLogBook::getVoterList() {
return _voterList;
}
Committee.cpp register voter method
void Committee::RegVoter(RegLogBook logs) {
Voter *newVoter = new Voter();
logs.getVoterList().push_back(newVoter); //The voter is not added to the list
}
I call this in my main()
Election *Election2018 = new Election();
Committee com1 = new Committee();
com1.RegVoter(Election2018->getRegLogBook());
RegLogBook::getVoterList()
returns your _voterList
by value . That means it copies the whole vector and returns it. Then you add elements to that copy.
To fix this, simply change your
vector<Voter*> RegLogBook::getVoterList()
to
vector<Voter*>& RegLogBook::getVoterList()
// ^ notice the reference part
There is another problem that I initially missed. In your Committee::RegVoter
method, you take the argument by value . That means the method will be invoked with a copy of your RegLogBook
. You should also change
void Committee::RegVoter(RegLogBook logs)
to
void Committee::RegVoter(RegLogBook& logs)
// again notice the reference ^
Thanks to Lightness Races in Orbit for pointing that out
Remember - to work on the original object, not a copy of it, you should pass it either by a reference or a pointer . By default you should prefer passing by reference, unless you have strong argument to use pointers
I believe the problem is that you return the voter list by-value, meaning that a copy of the voter list is returned:
vector<Voter*> RegLogBook::getVoterList() {
return _voterList;
}
So this line:
logs.getVoterList().push_back(newVoter);
will modify the local copy instead of the value being stored in the object instance.
Try modifying your code to return a reference (by changing the return type to be vector<Voter*>&
)
vector<Voter*>& RegLogBook::getVoterList() {
return _voterList;
}
or, maybe better, return a pointer:
vector<Voter*>* RegLogBook::getVoterList() {
return &_voterList;
}
I prefer the last alternative, as this makes it more obvious that the returned value can be modified.
From what I'm seeing your code shouldn't even compile: you're using the new
keyword, but you're saving its returned value into a Committee
, not into a Committee*
as you should. If you are using an overloaded new
operator, please post the whole code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.