gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:46:26 26 4
gpt4 key购买 nike

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

我有一个 player 类(包含一个 Score 和玩家的手,以 std::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 识别为有效类型...但为什么呢?我在 player.h 的开头正确包含了 Score.h:

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

我在 Score.h 中定义了 Score 的默认构造函数:

Score(); //score.h

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

如有任何意见,我们将不胜感激!

谢谢你的时间,

弗朗西斯科

编辑:

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

最佳答案

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

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


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


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

关于C++ 初学者 - 在类内部使用类时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2803910/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com