C++初学者 - 在类内使用类时遇到问题

发布于 2024-08-31 19:29:03 字数 1750 浏览 6 评论 0原文

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

我有一个 player 类(包含 Score 和玩家的手牌,以 std::string 的形式,以及一个 score 类( 。

Player 的成员函数之一是 Score getScore(),它返回一个分数 但是,我在编译时收到以下错误:

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 识别为有效类型...但是为什么?我已在 player.h 的开头正确包含 Score.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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只为守护你 2024-09-07 19:29:03

你遇到了循环包含问题 - 在这种情况下相对容易解决。

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


请参阅问题,了解如果您需要做什么的解释和讨论Score 实际上使用了 Player


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

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

智商已欠费 2024-09-07 19:29:03

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

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.

戴着白色围巾的女孩 2024-09-07 19:29:03

您遇到循环依赖问题Score.h 包含Player.hPlayer.h 包括 Score.h

要解决这个问题,请删除 Score.h 中对 Player.h 的包含,并定义 class Player;(如果您需要在 Score 中使用它)班级。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文