2011-07-26 49 views
1

嘿,我得到這個奇怪的錯誤,當我離開namespace sf{聲明在以後的代碼:「類模板已被宣佈爲一個非類模板」

1>c:\libraries and headers\sfml\sfml-1.6-sdk-windows-vc2008\sfml-1.6\include\sfml\graphics\body.h(70): error C2989: 'sf::Body' : class template has already been declared as a non-class template 
1>c:\libraries and headers\sfml\sfml-1.6-sdk-windows-vc2008\sfml-1.6\include\sfml\graphics\body.h(11): error C3856: 'sf': class is not a class template 

代碼工作正常時,它不是過去3周的模板類,具有相同的sf :: Body類名;我最近改變了它使它更加靈活。我可以不在名稱空間內聲明一個模板類或什麼?

下面的代碼:

#include <SFML/Graphics.hpp> 
#include <vector> 
#include <math.h> 
#include <cmath> 

namespace sf{ //when i take this out and the closing bracket the code runs fine 

    template<typename drawable>  
class Body : public sf::Drawable{ 

    private: 
     sf::Vector2f MoveVal; 
     std::vector<drawable> Drawables; 

    public: 
     Body(const Vector2f& Position = Vector2f(0, 0), const Vector2f& Scale = Vector2f(1, 1), float Rotation = 0.f, const Color& Col = Color(255, 255, 255, 255)){ 
      SetPosition(Position); 
      SetScale(Scale); 
      SetRotation(Rotation); 
      SetColor(Col);}; 

// Overide Drawable Functions To Detect any Movement 
     void SetX(float X){ 
      MoveVal.x += X - GetPosition().x; 
      Drawable::SetX(X);}; 

     void SetY(float Y){ 
      MoveVal.y += Y - GetPosition().y; 
      Drawable::SetY(Y);}; 

// Regular Functions 
     void AddObject(drawable& Object){ 
      Object.Move(GetX(),GetY()); 
      Drawables.push_back(Object);}; 

     void DestroyObject(unsigned short Index){ 
      Drawables.erase(Drawables.begin()+Index);}; 

     void Clear(){ 
      Drawables.clear();}; 

     drawable& GetObject(unsigned short index) 
      {return Drawables[index];}; 

     unsigned int GetNumbObjects() 
     {return Drawables.size();}; 

     void Draw(sf::RenderTarget& target){ 
      for(unsigned short I=0; I<Drawables.size(); I++){ 
       //Body offset 
       Drawables[I].SetPosition( 
        Drawables[I].GetPosition().x + MoveVal.x, 
        Drawables[I].GetPosition().y + MoveVal.y); 
      }             // TODO: add tint based on overall Body Color 

      target.Draw(*this); 

      //Reset all the Change Values 
      MoveVal.x=0; 
      MoveVal.y=0; 
     }; 

     void Render(sf::RenderTarget& target) const{ 
      for(int I=0; I< Drawables.size(); I++) 
       Drawables[I].Draw(target); 
     }; 
};// Body Class 

} //namespace sf 
+0

+1有趣。但是,在函數體之後你不需要';'。 – iammilind

+3

如果你在源代碼中加入清晰的評論,那麼我們就知道哪些行會報告那些錯誤......痛苦地計數或尋找或剪切粘貼跳轉就開始調查你的問題。乾杯。 –

+0

是的,我從「Sam's C++」中找到了這種習慣 – Griffin

回答

9

根據您的信息的兩種最可能的候選者是Graphics.hpp不匹配的{},或者你有的class Body預先聲明無標記它的模板。

+2

@Griffin:如果它以前不是模板,那肯定會贊成「前向聲明'class Body'沒有標記爲模板。「 –

+1

我發現了這個問題:我將Body聲明爲前一個類的朋友,但沒有將其指定爲模板。 – Griffin

4

sf::Body是一個似乎已經被採用的名稱(對於一個類而你聲明瞭一個模板)。您確定要將代碼放入sf名稱空間嗎?習慣於使用自己的命名空間,而不是使用自己的庫。

+0

代碼在過去3周內不是模板類時運行良好,具有相同的sf :: Body類名稱。所以我不認爲這是一個雙聲明的問題... – Griffin

+2

@Griffin:'typedef int X; typedef int X;'是完全有效的C++,那麼你去改變它'typedef int X; typedef double X;'你會得到一個錯誤。參數:*它在不同的情況下工作*在這裏不存在:這是因爲它現在不同了,雙聲明觸發錯誤。 –

4

確定發現了問題:

在先前包含頭文件:Shape.hpp我宣佈Body與下面的語法的朋友:

friend class Body; 

這顯然使編譯器承擔主體不是模板(沒有模板指示) 正確的語法是:

template <typename drawable>  
friend class Body; 

由於現在編譯器將Body理解爲模板類

+1

雖然這個答案是正確的,你應該接受其他人之一。他們告訴你完全一樣的東西,即使你不明白他們的意思,並不斷抱怨說**這不能成爲問題。 –

+0

猜測我不明白這是一個類的實際聲明,而不僅僅是一個朋友聲明,感謝您的幫助! – Griffin