2013-10-24 99 views
3

我得到std :: vector错误我从来没有听说过,无法找到任何有关它。C++矢量语法错误

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; 

这两条线路均相当长的一段时间完美的工作,我真的不知道是什么原因导致它突然开始做这些错误。 我还没有开始尝试填充矢量,并没有任何功能被称为尚未。

回答

3

您的两个头文件互相引用。但是,Shot.h对于ShootManager.h显然是必需的,因为在ShootManager中引用了Shot

因此,客户端程序#includes Shot.h或ShootManager.h是否有区别,以及它是否包括这两个顺序。如果Shot.h首先被包含,事情就会起作用。否则,他们不会,因为你不能使用未声明的标识符模板一个类。

我从Shot.h删除#include "ShootManager.h",然后修复任何结果打破(客户端代码中可能丢失的#include "ShootManager.h"

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

+0

只需在ShotManager.h中放入'class Shot;'而不是包含Shot.h. – kfsone

+0

@kfsone:你不能从一个不完整的类型中创建一个std :: vector,所以这不会有帮助。 – rici

+0

他有'矢量'而不是'矢量' – kfsone

2

错误与std::vector无关。这两个头文件之间有循环依赖关系。我建议在ShootManager头文件中向前声明Shot

// ShootManager.h 

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

此外,请避免将整个std命名空间带到标题。反而写using std::vector;或前缀std凡你使用vector