Reputation: 105
Here is my code, i have done multiple tests to make sure the adding of objects into vector is correct. So i try to advance from there a little more. My program is that a Voter class will a vector of BallotPaper, whereas a BallotPaper will have a vector of Candidate. The Candidate class will have a private variable called _votecount.
What I want to perform is that, I want to increase _votecount through Voter, using function Vote(int,int)
.
However in my case the _votecount does not increment as I have expected and I cant figured out, probably something obvious, thus I appreciate any inputs.
Here is my main:
int main()
{
Date d1(2018, 10, 1);// new Date object
Member m1("Alice", 100, "Engineering", 012, d1, PositionType::Normal);
bool check = m1.CheckEligibility();
cout << check<<"\n";
Voter v1("Ailee", 100, "Engineering", 012, d1, PositionType::Normal);
if (v1.getName() == "Ailee")
{
cout << "\nTrue1\n"; // Yes
}
BallotPaper bp1(PositionType::President);
v1.AddBallotPaper(bp1);
if (v1.getBallotPapers()[0].getPositionBP() == PositionType::President)
{
cout << "\nTrue2\n"; //Yes
}
Candidate c1("Ailee", 100, "Engineering", 012, d1, PositionType::Normal);
v1.getBallotPapers()[0].AddCandidate(c1);
if (v1.getBallotPapers()[0].getCandidates()[0].getName() == "Ailee")
{
cout << "\nTrue3\n"; //Yes
}
// Voter cast vote to increase candidate count
v1.Vote(0, 0);
if (c1.getVoteCount() == 1)
{
cout << "\nDONE\n"; //ERROR HERE!
}
return 0;
}
Voter class
class Voter :public Member
{
private:
std::vector<BallotPaper> _bp;
public:
Voter::Voter(std::string a, int b, std::string c, int d, Date e, PositionType f) : Member(a, b, c, d, e, f)
{}
void Voter::AddBallotPaper(BallotPaper b)
{
_bp.push_back(b);
}
std::vector<BallotPaper>& Voter::getBallotPapers()
{
return this->_bp;
}
//HOW DO YOU VOTE?
void Voter::Vote(int paperindex,int index)
{
getBallotPapers()[paperindex].ChoiceVoted(index);
}
}
BallotPaper class
class BallotPaper
{
private:
PositionType _positionbp;
std::vector<Candidate> _candidatesbp; // only contain the required candidate
public:
BallotPaper::BallotPaper(PositionType a)
{
_positionbp = a;
}
std::vector<Candidate>& BallotPaper::getCandidates()
{
return this->_candidatesbp;
}
void BallotPaper::AddCandidate(Candidate c)
{
_candidatesbp.push_back(c);
}
void BallotPaper::ChoiceVoted(int index)
{
getCandidates()[index].IncreaseVoteCount();
}
}
Candidate Class
class Candidate :public Member
{
private:
int _votecount;
PositionType _position;
public:
void Candidate::IncreaseVoteCount()
{
_votecount++;
}
}
Upvotes: 1
Views: 49
Reputation: 1049
The candidate created in your program is copied into the vector in Voter v
. You should check the count using v1.getBallotPapers()
.
An easier solution architecture would be to keep all Candidate
objects in a vector, and the other vectors store Candidate *
, so they can refer to the single definition of Candidate
.
Upvotes: 3