C++ vector 语法错误

标签 c++ vector syntax-error

我遇到了从未听说过的 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/

相关文章:

c++ - vector.push_back()在推送线程时(在编译过程中)产生已删除的函数错误

safari - jQuery.Deferred 异常 : The string did not match the expected pattern

mysql - 从 2 表到表 1 的列插入

c++ - 模拟以退出代码 : 132 终止

c++ - gsl::gsl_vector 与 std::vector 开销和效率

matrix - 深度学习模型中不同数量节点的直觉

php - 解析错误…根本找不到? (第86行)

c++ - 如果我想要最大速度,我应该只在 std::vector 上使用数组吗?

c++ - OpenGL 中用于 OBJ 导入的平滑着色器?

c++ - 单独的编译和模板显式实例化