“类模板已被声明为非类模板”

发布于 2024-11-25 23:37:09 字数 2788 浏览 0 评论 0原文

嘿,当我在后面的代码中保留 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

Hey i'm getting this odd error when I leave the namespace sf{ declaration in the later code:

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

The code worked fine when it wasn't a template class for the past 3 weeks, With the same sf::Body class name; i just recently changed it to make it more flexible. Can i not declare a template class inside a namespace or what?

Here's the code:

#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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔女人霸气范 2024-12-02 23:37:09

好的,发现了问题:

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

friend class Body;

这显然使编译器假设 Body 不是模板(未注明模板)
正确的语法是:

template <typename drawable>    
friend class Body;

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

Ok found the problem:

In a previously included header file: Shape.hpp I declare Body as a friend with the following syntax:

friend class Body;

Which apparently makes the compiler assume Body is not a template (no template indication is made)
The correct syntax was:

template <typename drawable>    
friend class Body;

Because now the compiler understands Body as a template class

墟烟 2024-12-02 23:37:09

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

The two most likely candidates based on your information are that Graphics.hpp has mismatched { } or that you had a forward declaration of class Body without marking it a template.

回忆追雨的时光 2024-12-02 23:37:09

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

sf::Body is a name that seems to be already taken (for a class whereas you're declaring a template). Are you sure you want to put your code in the sf namespace? It's more customary to use one's own namespaces rather than those of the libraries they use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文