gpt4 book ai didi

c++ - Visual C++ 模板语法错误

转载 作者:行者123 更新时间:2023-11-30 04:31:06 24 4
gpt4 key购买 nike

我有一个头文件...

#include <SFML\Graphics.hpp>
#include <SFML\Graphics\Drawable.hpp>
#include <SFML\System.hpp>
#include <iostream>

#ifndef _SPRITE_H_
#define _SPRITE_H_

namespace Engine
{
template <class T>
class Sprite
{
sf::Vector2<T> * vector;
sf::Sprite * sprite;

public:
Sprite(sf::Vector2<T> vect, sf::Sprite spr)
{
this->sprite = spr;
this->vector = vect;
}
~Sprite();
bool Draw(T x, T y, T rotate = 0);
sf::Image GetImage()
{
return this->sprite->GetImage();
}
};
};

#endif _SPRITE_H_

还有一个源文件...

#include <SFML/Graphics.hpp>
#include <SFML/Config.hpp>

#include "sprite.h"

template <typename T>
Sprite(sf::Vector2<T> vector, sf::Sprite sprite)
{
this->sprite = sprite;
this->vector = vector;
}

template <typename T>
bool Draw(T x, T y, T rotate)
{
return false;
}

在 VS 2010 中,当我编译 VC++ 时出现以下错误:

Error   2   error C2143: syntax error : missing ';' before '<'  c:\users\owner\documents\visual studio 2010\projects\engine2\engine2\sprite.cpp 7   1   Engine2
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\owner\documents\visual studio 2010\projects\engine2\engine2\sprite.cpp 7 1 Engine2
Error 4 error C2988: unrecognizable template declaration/definition c:\users\owner\documents\visual studio 2010\projects\engine2\engine2\sprite.cpp 7 1 Engine2
Error 5 error C2059: syntax error : '<' c:\users\owner\documents\visual studio 2010\projects\engine2\engine2\sprite.cpp 7 1 Engine2
Error 6 error C2059: syntax error : ')' c:\users\owner\documents\visual studio 2010\projects\engine2\engine2\sprite.cpp 7 1 Engine2
Error 7 error C2143: syntax error : missing ';' before '{' c:\users\owner\documents\visual studio 2010\projects\engine2\engine2\sprite.cpp 15 1 Engine2
Error 8 error C2447: '{' : missing function header (old-style formal list?) c:\users\owner\documents\visual studio 2010\projects\engine2\engine2\sprite.cpp 15 1 Engine2

我完全是 C++ 的菜鸟(来自 C#),并且在编译这个简单的文件时遇到了问题,作为一种至少在我继续之前学习语法的方法。如您所见,我所要做的就是将头文件中的模板引用到源文件中,以便模板引用影响我的所有方法。

我做错了什么?我试图理解这些编译器消息,但我无法解密它们。

更新

遵循建议:处理几乎所有事情,除了这个:

Error   1   error C2995: 'Engine::Sprite<T>::Sprite(sf::Vector2<T>,sf::Sprite)' : function template has already been defined    c:\users\owner\documents\visual studio 2010\projects\engine2\engine2\sprite.cpp 12  1   Engine2

最佳答案

因为SpriteEngine里面,所以应该是

template <typename T>
Engine::Sprite<T>::Sprite(sf::Vector2<T> vector, sf::Sprite sprite)
{ ... }

template <typename T>
bool Engine::Sprite<T>::Draw(T x, T y, T rotate)
{
return false;
}

或者,正如@jli 所建议的那样,也将它放在命名空间 Engine 中:

namespace Engine {
template <typename T>
Sprite<T>::Sprite(sf::Vector2<T> vector, sf::Sprite sprite)
{ ... }

template <typename T>
bool Sprite<T>::Draw(T x, T y, T rotate)
{
return false;
}
}

关于c++ - Visual C++ 模板语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8371732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com