"class template has already been declared as a non-class template"

"class template has already been declared as a non-class template"

本文关键字:template as non-class declared already class has been      更新时间:2023-10-16

嘿,当我在后面的代码中离开namespace sf{ 声明时,我得到了这个奇怪的错误:

1>c:libraries and headerssfmlsfml-1.6-sdk-windows-vc2008sfml-1.6includesfmlgraphicsbody.h(70): error C2989: 'sf::Body' : class template has already been declared as a non-class template
1>c:libraries and headerssfmlsfml-1.6-sdk-windows-vc2008sfml-1.6includesfmlgraphicsbody.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

Ok找到问题了:

在前面包含的头文件中:Shape.hpp我用以下语法声明Body为友元:

friend class Body;

这显然使编译器假设Body不是模板(没有模板指示)正确的语法是:

template <typename drawable>    
friend class Body;

因为现在编译器将Body理解为模板类

根据您的信息,两个最可能的候选是Graphics.hpp{ }不匹配,或者您有class Body的前向声明而没有将其标记为模板。

sf::Body是一个似乎已经采用的名称(对于一个类,而您正在声明一个模板)。您确定要将代码放在sf名称空间中吗?更习惯的做法是使用自己的命名空间,而不是使用库的命名空间。

相关文章: