0

我正在做一个大学项目,我必须在其中实现一个简单的拼字游戏。

我有一个player类(包含一个分数和玩家的手,形式为 astd::string和一个score类(包含名称和数字 ( int) 分数)。

Player成员函数之一是Score getScore(),它为该玩家返回一个 Score 对象。但是,我在编译时收到以下错误:

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

这里分别是第 27 和 35 行:

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

我知道编译器无法识别Score为有效类型......但是为什么呢?我Score.h在开头正确地包括了player.h

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

我有一个默认构造函数Score定义在Score.h

Score(); //score.h

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

任何输入将不胜感激!

谢谢你的时间,

弗朗西斯科

编辑:

根据要求, score.h 和 player.h: http ://pastebin.com/3JzXP36i http://pastebin.com/y7sGVZ4A

4

3 回答 3

7

你有一个循环包含问题 - 在这种情况下相对容易修复。

#include "Player.h"从中删除Score.h


有关实际使用时需要做什么的解释和讨论,请参阅问题。ScorePlayer


至于您遇到的编译器错误,这就是 Microsoft 的编译器报告使用未定义类型的方式 - 您应该学会在心理上将它们全部转换为“未定义声明中使用的类型”。

于 2010-05-10T15:38:31.743 回答
3

您的问题是递归包含Score.h正在尝试包含Player.h,并且Player.h正在尝试包含Score.h。由于 Score 类似乎实际上并未使用 Player 类,因此删除#include "Player.h"fromScore.h应该可以解决您的问题。

于 2010-05-10T15:41:14.557 回答
1

您有一个循环依赖问题Score.h包含Player.hPlayer.h包含Score.h

一定要解决这个问题,删除你的 include并定义你是否需要在 Score 类中使用它Player.hScore.hclass Player;

于 2010-05-10T15:42:25.277 回答