2011-07-26 51 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