0

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

JoeG
  • 12,994
  • 1
  • 38
  • 63
F. P.
  • 5,018
  • 10
  • 55
  • 80
  • Your first error probably occurs on line 26. Note that the error message indicates "before identifier." You may wish to include a bit more code. Also, constructors should be declared inside a class definition. – WhirlWind May 10 '10 at 15:29
  • I don't think so... That error is thrown because I attempt to define Score as the return type. I have declared constructors correctly. Any suggestions on what code I should include? – F. P. May 10 '10 at 15:31
  • 2
    You need to paste more code. Whole `score.h` and first lines of `player.h` – pajton May 10 '10 at 15:33
  • Hard to tell with just tiny snippets of code, but where is your class definition, and is Score defined when it's being used? – WhirlWind May 10 '10 at 15:34
  • Include a biger picture of Your code in Your question. If the compiler complains about missing semicolon it's most probably missing semicolon, but if it's not we have to see the code. One thing to learn about C++ is that the first error is the one to fix first. Often subsequent errors come form the first one. – Maciej Hehl May 10 '10 at 15:35

3 Answers3

7

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".

Community
  • 1
  • 1
JoeG
  • 12,994
  • 1
  • 38
  • 63
3

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.

JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169
1

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.

Julien Hoarau
  • 48,964
  • 20
  • 128
  • 117