gpt4 book ai didi

C++ vector 语法错误

转载 作者:太空狗 更新时间:2023-10-29 20:42:12 28 4
gpt4 key购买 nike

我遇到了从未听说过的 std::vector 错误,也找不到任何相关信息。

ShootManager.h

#pragma once

#include "VGCVirtualGameConsole.h"
#include "Shot.h"
#include <vector>

using namespace std;

class ShootManager
{
public:
ShootManager();
~ShootManager();

void Destroy(int ShotNr);
void Update();
void Draw();
void Fire(Shot* shot);

vector<Shot*> shots;
};

Shot.h

#pragma once

#include "VGCVirtualGameConsole.h"
#include "ShootManager.h"

using namespace std;

class Shot
{
public:
virtual ~Shot();
virtual void Update() = 0;
void Draw();
void Move();

enum Alignment
{
FRIEND, ENEMY
};

protected:
VGCVector position;
VGCVector speed;
Alignment alignment;
bool Destroyed = false;
};

我得到这些错误

Error   3   error C2059: syntax error : '>' 
Error 7 error C2059: syntax error : '>'
Error 1 error C2061: syntax error : identifier 'Shot'
Error 5 error C2061: syntax error : identifier 'Shot'
Error 2 error C2065: 'Shot' : undeclared identifier
Error 6 error C2065: 'Shot' : undeclared identifier
Error 4 error C2976: 'std::vector' : too few template arguments
Error 8 error C2976: 'std::vector' : too few template arguments

标识符错误是针对这一行

void Fire(Shot* shot);

休息

vector<Shot*> shots;

这两条线在很长一段时间内都运行良好,我真的不知道是什么原因导致它突然开始出现这些错误。我还没有开始尝试填充 vector ,目前还没有调用任何函数。

最佳答案

你的两个头文件相互引用。但是,Shot.h 显然是 ShootManager.h 所必需的,因为 ShotShootManager 中被引用。

因此,客户端程序是否#includes Shot.h 或 ShootManager.h 会有所不同,如果它#includes 两者,顺序是什么。如果首先#included Shot.h,一切都会正常进行。否则它们不会,因为您不能使用未声明的标识符来模板化类。

我会从 Shot.h 中删除 #include "ShootManager.h",然后修复任何中断的结果(可能是丢失的 #include客户端代码中的“ShootManager.h”。)

正如@kfsone 在评论中指出的那样,您还可以从 ShootManager.h 中删除 #include "Shot.h",将其替换为前向声明 射击类;。这样做会强制客户端代码同时包含 ShootManager.hShot.h(如果它们同时使用这两个类),因此可能需要更多修复,但这肯定是最干净的解决方案。

关于C++ vector 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19556890/

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