#election_day.cpp

11 messages · Page 1 of 1 (latest)

zinc forge
#

Hello, I'm struggling with trying to figure out where I'm going wrong with the Election Day C++ practice, below is my code:

#include <string>
#include <vector>

namespace election {

// The election result struct is already created for you:

struct ElectionResult {
    // Constructor
    ElectionResult(std::string nameIn, int voteIn): name(nameIn), votes(voteIn) {};
    // Name of the candidate
    std::string name{};
    // Number of votes the candidate has
    int votes{};
};

// TODO: Task 1
// vote_count takes a reference to an `ElectionResult` as an argument and will
// return the number of votes in the `ElectionResult.

int vote_count(ElectionResult& name){
    return name.votes;
}

// TODO: Task 2
// increment_vote_count takes a reference to an `ElectionResult` as an argument
// and a number of votes (int), and will increment the `ElectionResult` by that
// number of votes.

void increment_vote_count(ElectionResult& name, int incrementValue){
    name.votes += incrementValue;
}

// TODO: Task 3
// determine_result receives the reference to a final_count and returns a
// reference to the `ElectionResult` of the new president. It also changes the
// name of the winner by prefixing it with "President". The final count is given
// in the form of a `reference` to `std::vector<ElectionResult>`, a vector with
// `ElectionResults` of all the participating candidates.

ElectionResult determine_result(std::vector<ElectionResult>& final_count){
    int most_votes {0};
    int index_most_votes {0};
    for(int i = 0; i < final_count.size(); i++){
        if (final_count[i].votes > most_votes){
            most_votes = final_count[i].votes;
            index_most_votes = i;
        }
    }
    final_count[index_most_votes].name = "President " + final_count[index_most_votes].name;
    return final_count[index_most_votes];
}
}  // namespace election
#

Here's the error codes:

We received the following error when we ran your code:
/tmp/election-day/election_day_test.cpp: In function 'void CATCH2_INTERNAL_TEST_0()':
/tmp/election-day/election_day_test.cpp:11:27: error: no matching function for call to 'election::ElectionResult::ElectionResult(<brace-enclosed initializer list>)'
   11 |     ElectionResult result{};
      |                           ^
In file included from /tmp/election-day/election_day_test.cpp:1:
/tmp/election-day/election_day.cpp:10:5: note: candidate: 'election::ElectionResult::ElectionResult(std::string, int)'
   10 |     ElectionResult(std::string nameIn, int voteIn): name(nameIn), votes(voteIn) {};
      |     ^~~~~~~~~~~~~~
/tmp/election-day/election_day.cpp:10:5: note:   candidate expects 2 arguments, 0 provided
/tmp/election-day/election_day.cpp:8:8: note: candidate: 'election::ElectionResult::ElectionResult(const election::ElectionResult&)'
    8 | struct ElectionResult {
      |        ^~~~~~~~~~~~~~
/tmp/election-day/election_day.cpp:8:8: note:   candidate expects 1 argument, 0 provided
/tmp/election-day/election_day.cpp:8:8: note: candidate: 'election::ElectionResult::ElectionResult(election::ElectionResult&&)'
/tmp/election-day/election_day.cpp:8:8: note:   candidate expects 1 argument, 0 provided
/tmp/election-day/election_day_test.cpp: In function 'void CATCH2_INTERNAL_TEST_4()':
/tmp/election-day/election_day_test.cpp:25:27: error: no matching function for call to 'election::ElectionResult::ElectionResult(<brace-enclosed initializer list>)'
   25 |     ElectionResult result{};
      |                           ^
/tmp/election-day/election_day.cpp:10:5: note: candidate: 'election::ElectionResult::ElectionResult(std::string, int)'
   10 |     ElectionResult(std::string nameIn, int voteIn): name(nameIn), votes(voteIn) {};
      |     ^~~~~~~~~~~~~~
#
/tmp/election-day/election_day.cpp:10:5: note:   candidate expects 2 arguments, 0 provided
/tmp/election-day/election_day.cpp:8:8: note: candidate: 'election::ElectionResult::ElectionResult(const election::ElectionResult&)'
    8 | struct ElectionResult {
      |        ^~~~~~~~~~~~~~
/tmp/election-day/election_day.cpp:8:8: note:   candidate expects 1 argument, 0 provided
/tmp/election-day/election_day.cpp:8:8: note: candidate: 'election::ElectionResult::ElectionResult(election::ElectionResult&&)'
/tmp/election-day/election_day.cpp:8:8: note:   candidate expects 1 argument, 0 provided
/tmp/election-day/election_day_test.cpp: In function 'void CATCH2_INTERNAL_TEST_10()':
/tmp/election-day/election_day_test.cpp:49:46: error: cannot bind non-const lvalue reference of type 'election::ElectionResult&' to an rvalue of type 'election::ElectionResult'
   49 |     ElectionResult& result = determine_result(final_count);
      |                              ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
/tmp/election-day/election_day_test.cpp: In function 'void CATCH2_INTERNAL_TEST_12()':
/tmp/election-day/election_day_test.cpp:60:46: error: cannot bind non-const lvalue reference of type 'election::ElectionResult&' to an rvalue of type 'election::ElectionResult'
   60 |     ElectionResult& result = determine_result(final_count);
      |                              ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
/tmp/election-day/election_day_test.cpp: In function 'void CATCH2_INTERNAL_TEST_14()':
/tmp/election-day/election_day_test.cpp:74:46: error: cannot bind non-const lvalue reference of type 'election::ElectionResult&' to an rvalue of type 'election::ElectionResult'
   74 |     ElectionResult& result = determine_result(final_count);
      |                              ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
#
make[2]: *** [CMakeFiles/election-day.dir/build.make:76: CMakeFiles/election-day.dir/election_day_test.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/election-day.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

I'm normally pretty good at being able to debug, but this one has me really stumped. The first error code makes it look like i'm not allowing my constructor to initialize a list of Election Results (my implementation of individual name and votes would not be sufficient then?).

#

Also, is there somewhere I can check the arguments that are being passed for the program's validation?

dense raptor
#

With C++ I strongly recommend focussing on the first error message. All other errors might depend on and be caused by the that first one.

#
election_day_test.cpp:11:27: error: no matching function for call to 'election::ElectionResult::ElectionResult(<brace-enclosed initializer list>)'
   11 |     ElectionResult result{};
      |                           ^
#

That means that the tests (election_day_test.cpp, line 11) try to create an ElectionResult by calling the default constructor (without arguments, {}).

#

But in your solution you added constructor that takes two arguments, so the compiler no longer creates a default constructor.

#

The result: ElectionResult result{}; can no longer default-construct an ElectionResult, you get an error.

#

I'd suggest deleting this constructor from ElectionResult.