1

im a noob especially in c++.

I have some problems using a struct that i want to use in some functions.

I looked at the other answers here regarding structs in Header files and .cpp but dont realy get it to work.

my .h file:

namespace game {

class CPlayer{

        struct GameData{
        int mBoard[8][8];
        CBoard &pBoard;
    };
public:

    CMove Play(const CBoard &pBoard,const CDeadline &pDue);

private:

    int GainsFromMoves(struct GameData gameData);

etc.....

then in my .cc file im trying to use this data with:

CMove CPlayer::Play(const CBoard &pBoard,const CDeadline &pDue)
{

     GameData gameData;

     gameData.pBoard = pBoard;

etc...

where i try to declare the GameData i get an error uninitialized reference member in ‘struct game::CPlayer::GameData’

later trying to usethe GainsFromMoves function i also get some error.

int GainsFromMoves(GameData gameData){

 int test = 0;

 return test;
}

error: Multiple markers at this line
    - expected ‘,’ or ‘;’ before ‘{’ token
    - ‘GameData’ was not declared in this 
     scope

i realize that i probably am doing some really noob error but, i would be very happy for some guidance.

jam
  • 3,640
  • 5
  • 34
  • 50
user1497664
  • 23
  • 1
  • 3
  • 3
    A reference member must be initialized in [member initializer list](http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor). – Alok Save Sep 12 '12 at 07:02
  • In general, try to narrow your problems down. This has nothing to do with headers and cc. files, which you would have found out by making a five-line test program to reproduce the problem. – juanchopanza Sep 12 '12 at 07:09
  • I think it's quite possible your code is confused, and that what you are trying to achieve is actually quite a long way from what you have currently. But as a first step I would move the declaration of `GameData` outside of the declaration of `CPlayer`, i.e. `struct GameData { ... }; class CPlayer { ... };`. Is there any reason you declared one inside the other? I would also drop completely `namespace game ...`. At your level of understanding it's just going to make things more complicated without providing any benefit. – john Sep 12 '12 at 07:10
  • The second error is because compiler does not know the *type*, `GameData`.You need to include the header file which defines `GameData` in the source file where you have defined the function `GainsFromMoves()`. – Alok Save Sep 12 '12 at 07:10
  • I think @juanchopanza advice is good. You need to start again, build up your code slowly, adding tiny bits at a time, making sure it at least compiles as you go. Seems to me you are trying too much too quickly. – john Sep 12 '12 at 07:15
  • @Als I think he has. I think that specific problem is because he's written a free function when he meant to define the member function. I.e. if he wrote `int CPlayer::GainsFromMoves(GameData gameData) {` it would be OK. – john Sep 12 '12 at 07:18
  • @john: I doubt that.If that would have been the case the error would be undefined reference to the member function and not `‘GameData’ was not declared in this scope`.Defining a freestanding function would not yield the errors mentioned in the OP. – Alok Save Sep 12 '12 at 07:21
  • There is way too much missing here. Where are `CMove`, `CBoard` and `CDeadline` declared? – juanchopanza Sep 12 '12 at 07:24
  • @Als You are missing `GameData` is declared inside `CPlayer`. `int GainsFromMoves(GameData gameData){` does not compile because the compiler is looking for `GameData` in the global scope. – john Sep 12 '12 at 07:31

1 Answers1

2

OK, first, you need declarations of CMove, CBoard and CDeadline. I added some dummy ones to allow fixing the other problems:

namespace game {

struct CBoard {};
struct CDeadline {};
struct CMove {};
...

Then, your struct CPlayer::GameData needs to intialize it's CBoard reference, which should be const:

class CPlayer{

        struct GameData
        {
          GameData(const CBoard& board) : pBoard(board) {}
          int mBoard[8][8];
          const CBoard &pBoard;
        };
public:
    CMove Play(const CBoard &pBoard,const CDeadline &pDue);
private:
    int GainsFromMoves(struct GameData gameData);
};

Next, in your .cc file, you need the namespace as well as the class name when defining member functions:

namespace game
{
CMove CPlayer::Play(const CBoard &pBoard,const CDeadline &pDue)
{
     GameData gameData(pBoard);
     return CMove();
}
int CPlayer::GainsFromMoves(GameData gameData){

  int test = 0;

 return test;
}

} // namespace game

This gets rid of the errors, at least with this simple main:

int main()
{
  game::CBoard b;
  game::CDeadline d;
  game::CPlayer p;
  p.Play(b, d);
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • This assumes that his design is basically correct, and all that the OP needs is advice with the technical aspects of implementing it. I'm not sure that is correct. But a very useful answer if it's what the OP needs. – john Sep 12 '12 at 07:34
  • @john You are right, I am not going into the design at all. I think there isn't enough information in the question yet. A lot of the code I posted is just dummy code to make things compile. – juanchopanza Sep 12 '12 at 07:38
  • Thanks that helped but im still at square one(the reason i started all this mess with struct) When writing like you it works in my play function but in my GainsFromMoves i get errors. so in the header it is: int GainsFromMoves(struct GameData gameData); in the .cc file its: int GainsFromMoves(GameData gameData) but then i get the following : Multiple markers at this line - ‘GameData’ was not declared in this scope - expected ‘,’ or ‘;’ before ‘{’ token - Type 'GameData' could not be resolved – user1497664 Sep 12 '12 at 08:16
  • Hmm well moved the struct to declare it in the .cc file, combined with some of your help it made it compile at least, now i will try to see if it works as well. Again thanks – user1497664 Sep 12 '12 at 08:21