Hey everyone,

Just trying to add to a scoreboard from a different class. The problem is that i'm not sure how to add to a scoreboard that has been previously defined in a different class. As i've instantiatied i'm assuming that has made a whole new Game (with a new scoreboard) so i'm guessing that i need to use the keyword this... Anyhelp will be appreciated

Game {

  struct PlayerScore {
            string name;
            int score;
        } noah, bam; 


    vector<PlayerScore> scores;

    void Game::setup()
    {

        noah.name = "Noah";
        noah.score = 300;

        bam.name = "Bam";
        bam.score = 1000;

        scores.push_back(noah);
        scores.push_back(bam);
    }




        void Game::HallofFame() 
        {
            int temp = 1;

            sort(scores.begin(), scores.end(), &Game::sortbyScore);


            for (vector<PlayerScore>::iterator it = scores.begin(); it != scores.end(); ++it)
            {
                cout << temp << ". " << it->name << " - " << it->score << "\n";
                temp++;
            }

            cout << "\n";

        }


        void Game::addtoHallofFame(Adventure a)
        {   
            string playerName;
            int adventureScore = a.getScore();

            cout << "\nPlease enter you name: ";
            cin >> playerName;

            PlayerScore Player;
            Player.name = playerName;
            Player.score = adventureScore;

            cout << Player.name << " ";
            cout << Player.score;
            cout << "\n\n";


            scores.push_back(Player);

            HallofFame();

        }
}






Adventure {

    int score = 0;

    void Adventure::update(string arr[])
    {       
                if ((arr[0] == "quit"))
                {           
                    Game g;

                    g.addtoHallofFame(*this);

                }
    }
}

The problem with

{
    Game g;
    g.addtoHallofFame(*this);
}

is that g is a local variable at block scope (automatic storage duration); it will be destroyed when the scope is exited from.

One option would be to make vector<PlayerScore> scores; a static member of Game; this is logical if there can be only one game in progress at a time.

Another is to keep a reference to the associated Game as a member in the Adventure object.

Or to pass a reference to the associated Game as a parameter to update()

// favour a std::vector<std::string> or std::array< std::string, N > over raw arrays
// void Adventure::update( string arr[], Game& g )
void Adventure::update( const vector<string>& arr, Game& g )
{       
    if ( !arr.empty() && arr[0] == "quit" ) // ideally avoid the magic constant "quit"  
    {           
        g.addtoHallofFame(*this);
    }
}

Thanks for the reply.

I first tried the passing a reference to the associated Game as a parameter to update() method and thus i had to instantiate Game g into the Adventure.h file

class Adventure 
{

public:

    Game g;
    ...
};

which would mean that i had to include "Game.h".

#ifndef Adventure_h
#define Adventure_h

#include "Game.h"

Here's the Adventure.h...

#ifndef Adventure_h
#define Adventure_h

#include "Game.h"

#include <string>
#include <iostream>
#include <vector>
#include <sstream>

using namespace std;

class Adventure 
{

public:

    Game g;

    void update(string text[]);
    void input();

private:

    int score;
    ...


};


#endif

However, when this is done i'd get errors of...

game.h(20): error C2146: syntax error : missing ';' before identifier 'adv'
game.h(20): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
game.h(33): error C2061: syntax error : identifier 'Adventure'
adventure.cpp(211): error C2660: 'Game::addtoHallofFame' : function does not take 1 arguments

adventure.h(20): error C2146: syntax error : missing ';' before identifier 'g'
adventure.h(20): error C4430: missing type specifier - int assumed. Note: C++ does not support default-in

adventure.h(20): error C2146: syntax error : missing ';' before identifier 'g'
adventure.h(20): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

These errors do not appear if i do not #include "Game.h" in Adventure.h and the program would run.

#ifndef Game_h
#define Game_h

#include "Adventure.h"

#include <vector>
#include <string>
#include <iostream>
#include <algorithm> //sort

using namespace std;

class Game
{


public:

    Adventure adv;

    struct PlayerScore {
        string name;
        int score;
    } noah, bam; 

    static vector<PlayerScore> scores;

    void addtoHallofFame(Adventure a);
    void HallofFame();
    ...

private:                
    ...
};


#endif

I tried your other method of static vector vector but it seems an error would occur...

i changed vector<PlayerScore> scores; to static vector<PlayerScore> scores; but then an error occured

"unresolved external symbol "public: static class std::vector<struct Game::PlayerScore,class std::allocator<struct Game::PlayerScore> > Game::scores" (?scores@Game@@2V?$vector@UPlayerScore@Game@@V?$allocator@UPlayerScore@Game@@@std@@@std@@A)"

I understand this may be confusing but if you need more details of code please let me know.

Read this first: http://www.cplusplus.com/articles/Gw6AC542/
Take your time; make sure you have understood it.

The structure of your code should be something like this:

file game.h

#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

#include <string>
#include <vector>

class adventure ; // declare

class game
{
     struct PlayerScore {
            std::string name;
            int score;
     };

    std::vector<PlayerScore> scores ;

    public:

        // ...

        void addtoHallofFame( adventure a ) ; // declare, do not define

        // ...

};

#endif // GAME_H_INCLUDED

file adventure.h

#ifndef ADVENTURE_H_INCLUDED
#define ADVENTURE_H_INCLUDED

#include <vector>
#include <string>

class game ; // declare

class adventure {

    int score = 0 ;
    game& g ;

    public:
        adventure( game& the_game ) : g(the_game) { /* ... */ }

        // ...

        inline int getScore() const { return score ; }

        void update( const std::vector<std::string>& arr ) ; // declare, do not define

        // ...
        static const std::string quit_msg ; // this is just a declaration
};

#endif // ADVENTURE_H_INCLUDED

file game.cpp

#include "game.h"
#include "adventure.h" // class adventure is now defined

// ...

void game::addtoHallofFame( adventure a ) // define
{
    int adventureScore = a.getScore();

    std::string playerName;

    // ...

    PlayerScore p = { playerName, adventureScore } ;
    scores.push_back(p) ;
}

// ...

file adventure.cpp

#include "adventure.h"
#include "game.h" // class game is now defined

// ...

const std::string adventure::quit_msg = "quit" ; // define the static member in the cpp file

void adventure::update( const std::vector<std::string>& arr )
{

    if( !arr.empty() && arr.front() == quit_msg )
    // ...
    g.addtoHallofFame(*this) ;
}

// ...

The declaration of a static data member inside the class body is not a definition; it needs to be defined, once, in the .cpp file. There is an example above in adventure::quit_msg
More information: http://en.cppreference.com/w/cpp/language/static

Your method worked like a charm. Thank you very much.

If i wanted to change my code a bit...

#ifndef Game_H_INCLUDED
#define Game_H_INCLUDED

#include <vector>
#include <string>
#include <iostream>

using namespace std;

class Adventure;


class Game
{
    Adventure &adv;

public:

    struct PlayerScore {
        string name;
        int score;
    } noah, bam; 

    void addtoHallofFame(Adventure adv);

};


#endif

The reason i want to do this is because i don't want to use Adventure as an argument for my other functions. It's not necessary but it'd be nice to do. When i do this i'd get an error of

int main(int argc, const char * argv[])
{
    Game game;


    do {
        game.input();
        game.update();
        game.render();

    } while (true);


    return 0;
}

main.cpp(7): error C2512: 'Game' : no appropriate default constructor available

I assume this means that i need a constructor for Game but the compiler couldn't find one. i tried making an empty constructor

class Adventure;

class Game
{
    Adventure &adv;

public:
    Game():
    {}

but that just resulted in
1>...game.h(21): error C2059: syntax error : '{'
1>...game.h(21): error C2758: 'Game::adv' : must be initialized in constructor base/member initializer list
1>...game.h(17) : see declaration of 'Game::adv'

It seems to me that you either need a default constructor for the Adventure class or initialize it according to the existing constructor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.