views:

154

answers:

3

Hello,

I am working on a college project, where I have to implement a simple Scrabble game.

I have a player class (containing a Score and the player's hand, in the form of a std::string, and a score class (containing a name and numeric (int) score).

One of Player's member-functions is Score getScore(), which returns a Score object for that player. However, I get the following error on compile time:

player.h(27) : error C2146: syntax error : missing ';' before identifier 'getScore'
player.h(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
player.h(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
player.h(27) : warning C4183: 'getScore': missing return type; assumed to be a member function returning 'int'
player.h(35) : error C2146: syntax error : missing ';' before identifier '_score'
player.h(35) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
player.h(35) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Here's lines 27 and 35, respectively:

Score getScore(); //defined as public
(...)
Score _score; //defined as private

I get that the compiler is having trouble recognizing Score as a valid type... But why? I have correctly included Score.h at the beginning of player.h:

#include "Score.h"
#include "Deck.h"
#include <string>

I have a default constructor for Score defined in Score.h:

Score(); //score.h

//score.cpp
Score::Score()
{
    _name = "";
    _points = 0;
}

Any input would be appreciated!

Thanks for your time,

Francisco

EDIT:

As requested, score.h and player.h: http://pastebin.com/3JzXP36i http://pastebin.com/y7sGVZ4A

+7  A: 

You've got a circular inclusion problem - relatively easy to fix in this case.

Remove the #include "Player.h" from Score.h.


See this question for an explanation and discussion of what you'd need to do if Score actually used Player.


As for the compiler errors you're getting, that's how Microsoft's compiler reports the use of undefined types -you should learn to mentally translate them all into "Type used in declaration not defined".

Joe Gauterin
+1 - @Joe that'll be it.
ChrisBD
Thanks for that link.
Francisco P.
+3  A: 

Your problem is the recursive include: Score.h is trying to include Player.h, and Player.h is trying to include Score.h. Since the Score class doesn't seem to actually be using the Player class, removing #include "Player.h" from Score.h should solve your problem.

JSBangs
A: 

You have a circular dependency problem : Score.h includes Player.h and Player.h includes Score.h.

Do solve this problem, delete your include to Player.h in Score.h and define class Player; if you need to use it in Score class.

madgnome