2013-10-24 76 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