Issue with creating an array of objects in my project: expected unqualified-id





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







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;
}
};









share|improve this question

























  • Competitor listOfCompetitors = new Competitor[10]; -- C++ is not Java. If you want a dynamic array -- std::vector<Competitor> listOfCompetitors(numberOfCompetitors);

    – PaulMcKenzie
    Nov 25 '18 at 5:28




















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;
}
};









share|improve this question

























  • Competitor listOfCompetitors = new Competitor[10]; -- C++ is not Java. If you want a dynamic array -- std::vector<Competitor> listOfCompetitors(numberOfCompetitors);

    – PaulMcKenzie
    Nov 25 '18 at 5:28
















0












0








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;
}
};









share|improve this question
















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;
}
};






c++ arrays c++11






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 8:13









Matthieu Brucher

17.8k52445




17.8k52445










asked Nov 25 '18 at 5:20









Bill J.Bill J.

11




11













  • Competitor listOfCompetitors = new Competitor[10]; -- C++ is not Java. If you want a dynamic array -- std::vector<Competitor> listOfCompetitors(numberOfCompetitors);

    – PaulMcKenzie
    Nov 25 '18 at 5:28





















  • Competitor listOfCompetitors = new Competitor[10]; -- C++ is not Java. If you want a dynamic array -- std::vector<Competitor> listOfCompetitors(numberOfCompetitors);

    – PaulMcKenzie
    Nov 25 '18 at 5:28



















Competitor listOfCompetitors = new Competitor[10]; -- C++ is not Java. If you want a dynamic array -- std::vector<Competitor> listOfCompetitors(numberOfCompetitors);

– PaulMcKenzie
Nov 25 '18 at 5:28







Competitor listOfCompetitors = new Competitor[10]; -- C++ is not Java. If you want a dynamic array -- std::vector<Competitor> listOfCompetitors(numberOfCompetitors);

– PaulMcKenzie
Nov 25 '18 at 5:28














1 Answer
1






active

oldest

votes


















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.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464875%2fissue-with-creating-an-array-of-objects-in-my-project-expected-unqualified-id%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.






    share|improve this answer




























      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.






      share|improve this answer


























        0












        0








        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 6:56









        TrebledJTrebledJ

        4,04821432




        4,04821432
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464875%2fissue-with-creating-an-array-of-objects-in-my-project-expected-unqualified-id%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini