“类模板已经被声明为非类模板” [英] "class template has already been declared as a non-class template"
本文介绍了“类模板已经被声明为非类模板”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我离开命名空间的sf {
声明时,我得到这个奇怪的错误:
1> c:\libraries和headers\sfml\sfml-1.6-sdk-windows-vc2008 \sfml-1.6\include \\ sfml \graphics\body.h(70):错误C2989:'sf :: Body':类模板已被声明为非类模板
1> c:\libraries和headers \\ \\ sfml \sfml-1.6-sdk-windows-vc2008 \sfml-1.6 \include\sfml\graphics\body.h(11):错误C3856:'sf':类不是类模板
代码在过去3周不是模板类时工作得很好, :: Body类名;我最近改变它,使它更灵活。我可以不声明一个模板类在命名空间或什么?
代码如下:
#include< SFML / Graphics.hpp>
#include< vector>
#include< math.h>
#include< cmath>
命名空间sf {//当我把这个和最后括号的代码运行良好
template< typename drawable>
class Body:public sf :: Drawable {
private:
sf :: Vector2f MoveVal;
std :: vector< drawable>可绘制;
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);};
//覆盖可绘制函数以检测任何移动
void SetX(float X){
MoveVal.x + = X - GetPosition()。
Drawable :: SetX(X);};
void SetY(float Y){
MoveVal.y + = Y - GetPosition()。y;
Drawable :: SetY(Y);};
//常规函数
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:根据整体颜色添加色调
target.Draw(* this);
//重置所有更改值
MoveVal.x = 0;
MoveVal.y = 0;
};
void Render(sf :: RenderTarget& target)const {
for(int I = 0; I Drawables [I]。绘制(目标)
};
}; // Body Class
} //命名空间sf
Graphics.hpp 不匹配
{ code> }
或者你有一个向前声明 class Body
p>
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
解决方案 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.
这篇关于“类模板已经被声明为非类模板”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文