C++初学者 - 在类内使用类时遇到问题
我正在做一个大学项目,我必须实现一个简单的拼字游戏。
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你遇到了循环包含问题 - 在这种情况下相对容易解决。
从
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"
fromScore.h
.See this question for an explanation and discussion of what you'd need to do if
Score
actually usedPlayer
.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".
您的问题是递归包含:
Score.h
试图包含Player.h
和Player.h
正在尝试包含Score.h
。由于 Score 类似乎并未实际使用 Player 类,因此从Score.h
中删除#include "Player.h"
应该可以解决您的问题。Your problem is the recursive include:
Score.h
is trying to includePlayer.h
, andPlayer.h
is trying to includeScore.h
. Since the Score class doesn't seem to actually be using the Player class, removing#include "Player.h"
fromScore.h
should solve your problem.您遇到循环依赖问题:
Score.h
包含Player.h
和Player.h
包括Score.h
。要解决这个问题,请删除
Score.h
中对Player.h
的包含,并定义class Player;
(如果您需要在 Score 中使用它)班级。You have a circular dependency problem :
Score.h
includesPlayer.h
andPlayer.h
includesScore.h
.Do solve this problem, delete your include to
Player.h
inScore.h
and defineclass Player;
if you need to use it in Score class.