gpt4 book ai didi

c++ - 充斥着标识符错误

转载 作者:搜寻专家 更新时间:2023-10-31 02:08:19 24 4
gpt4 key购买 nike

我一直在为期末项目编写大富翁游戏。所以我以为我在滚动,并且我已经用我的伪代码弄清楚了一切。但是,我似乎忘记了如何正确处理包含,我知道这是问题所在,因为我能够将其完善到那个程度,但我不确定如何解决它。

在我的代码的这个 super 精简版本中,我有三个 .h 文件“Space.h”,这是一个抽象/虚拟类,必须由各种不同的空间继承,可以出现在典型的大富翁棋盘上:属性(property)、 jail 、税收、机会、公益金等。必须继承的功能是 run(Player&),这就是当您降落在棋盘上的特定空间时“运行”的内容,所有使用 run 的函数都使用通过参数传递的播放器。

#pragma once
#include <string>
#include "Player.h"

class Space
{
public:
virtual void run(Player&) = 0;
};

我的第二个 .h 文件是继承自 Space 的“Property.h”

#pragma once
#include "Space.h"

class Property : Space
{
public:
void run(Player&) override;
int i{ 0 };
};

最后我有“Player.h”,它有两个变量,一个名称和一个它拥有的属性 vector 。

#pragma once
#include <string>
#include <vector>
#include "Property.h"

class Player
{
public:
std::string name{ "foo" };
void addProperty(Property p);
private:
std::vector <Property> ownedProperties;
};

这是一个非常基本的属性实现

#include "Property.h"
#include <iostream>

void Property::run(Player & p)
{
std::cout << p.name;
}

播放器实现

#include "Player.h"
#include <iostream>

void Player::addProperty(Property p)
{
ownedProperties.push_back(p);
}

最后是主要内容

#include "Player.h"
#include "Space.h"
#include "Property.h"

int main()
{
Player p{};
Property prop{};
prop.run(p);
system("pause");
}

每次运行时我都会遇到很多错误,我确信它必须对循环包含逻辑做一些事情,玩家包括属性,属性包括空间,其中包括玩家。但是,我没有看到考虑到需要 #include 来了解所有内容是如何定义的解决方法不是吗?还是这些错误指的是其他东西?

enter image description here

最佳答案

你有一个循环包含问题。 Player 包含 Property,其中包含 Space,后者又包含 Player。

你可以通过在 Space.h 中不包含 Player.h 并且只向前声明类来打破循环

#pragma once

class Player;

class Space
{
public:
virtual void run(Player&) = 0;
};

关于c++ - 充斥着标识符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47706978/

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