0

The purpose of this (obviously incomplete) project is just some practice with objects in my free time. I have run into the following error when testing the project and I am not sure exactly what the issue could be.

"expected unqualified-id before ‘[’ token Competitor[] listOfCompetitors = new Competitor[10];"

Any help is much appreciated.

main.cpp

#include <iostream>
#include <string>
#include competitor.cpp;

using namespace std;
int scoringMethod;
int numberOfCompetitors;

int main(int argc, char** argv){
cout<<"How would tou like to score the competition? (please enter an interger)"<<endl;
cout<< "1. Closest Total Points"<<endl<<"2. Closest Total Points Without Going Over" <<endl<<"3. Closest to team1 Score"<<endl<< "4. Closest to Opponent Score"<<endl<<endl;
cin>>scoringMethod;
cout<<endl<<"How many competitors do you have?"<<endl;
cin>>numberOfCompetitors;
Competitor[] listOfCompetitors = new Competitor[10];
string tempName;
int tempScore1, tempScore2;
for (int i = 0; i<numberOfCompetitors;i++){

    cout<<"Name of competitor number "<< i<<"?"<<endl;
    cin>>tempName;
    cout<<tempName<<"'s prediction for team1's score?"<<endl;
    cin>>tempScore1;
    cout<<tempName<<"'s prediction for the score of team1's opponent?"<<endl;
    cin>>tempScore2;

    listOfCompetitors[i] = new Competitor(tempName,tempScore1,tempScore2);
}    
cout <<endl<<"The program has reached the end successfully" << endl;
}

competitor.cpp

#include <iostream>
#include <string>

using namespace std;

class Competitor{
private:
string Name;
int team1Score;
int opponentScore;

public:
Competitor(){
    Name = "invalid";
    team1Score = opponentScore = 0;
}
Competitor(string nameIn, int inteam1Score, int inOpponentScore){
    Name=nameIn;
    team1Score=inteam1Score;
    opponentScore=inOpponentScore;
}
void printData(){
    cout<<this->Name<<"'s guess:"<<endl<<"team1: "<<team1Score<< "     Opponent: "<<opponentScore<<endl;
}
};
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
Bill J.
  • 1
  • 1
  • `Competitor[] listOfCompetitors = new Competitor[10];` -- C++ is not Java. If you want a dynamic array -- `std::vector listOfCompetitors(numberOfCompetitors);` – PaulMcKenzie Nov 25 '18 at 05:28

1 Answers1

0
Competitor[] listOfCompetitors = new Competitor[10];

This is syntactically incorrect (for C/C++, at least) and is the reason why you're getting the error. If you want to dynamically allocate an array of Competitors, you could do

Competitor *listOfCompetitors = new Competitor[10];

and this will dynamically allocate an array of 10 Competitors.


A couple other issues with your code:

  1. You're looping from {0 ... numberOfCompetitors} and accessing your listOfCompetitors array at each turn. What if numberOfCompetitors ≥ 10? Accessing listOfCompetitors[i] for i ≥ 10 becomes UB, or undefined behaviour.

  2. The following line will cause a memory leak, if at all compile.

    listOfCompetitors[i] = new Competitor(tempName,tempScore1,tempScore2);

The type of listOfCompetitors[i] is Competitor, but new Competitor(...) will return a type of Competitor* (pointer to a Competitor). Thus, the new here isn't necessary. This should suffice:

listOfCompetitors[i] = Competitor(tempName, tempScore1, tempScore2);
  1. You're not deleting any dynamically-allocated memory. Every time you use new, you need to remember to use delete, or else there will be memory-leaks. (There are certain exceptions to this, but not in the majority of C++.) You'll want to deallocate listOfCompetitors.

So after you've finished using listOfCompetitors (after the for-loop, maybe), do

delete []listOfCompetitors;
  1. #include competitor.cpp; that shouldn't compile . Use quotations marks around non-standard-library filenames you're including: #include "competitor.cpp".

As suggested in the comments, you can make use of std::vector or std::array STL containers. They're easier to work with when it comes to dynamic arrays and do all the memory-management for you. (Also because you tagged your question with C++11 , STL containers would be a solid place to work towards.)

I strongly suggest picking up a C++ book to read and/or taking online tutorials to nail the fundamentals of dynamically-allocating memory, arrays, and whatnot.

TrebledJ
  • 8,713
  • 7
  • 26
  • 48