"class template has already been declared as a non-class template"
"class template has already been declared as a non-class template"
嘿,当我在后面的代码中离开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
名称空间中吗?更习惯的做法是使用自己的命名空间,而不是使用库的命名空间。
相关文章:
- 表示"accepting anything for this template argument" C++概念的通配符
- 传递给std::function template的template参数究竟代表什么
- 在template中使用std::variant的template函数
- 为什么我会" void value not ignored as it ought to be"?
- C++ template for QList
- C++ - 为什么这里需要'template'关键字?
- 使用"std::enable_if_t" "function template has already been defined"
- 为什么 std::shared_ptr 被认为是"heavy"和"expensive",但 std::array "same perfprmance as plain (c-style) arrays
- C++模板错误:"invalid explicitly-specified argument for template parameter"
- C++ class template
- 了解 Linux 虚拟内存:valgrind 的 massif 输出显示了有和没有 --pages-as-heap 的主要差异
- 在"template"和函数声明之间使用:template<typename trait> using tr = base_trait<trait> void fn(tr::t
- 为什么编译器说"candidate template ignored: couldn't infer template argument 'InputIterator'"?
- 编译错误:"lvalue required as left operand of assignment" 不确定原因
- 为什么在这种情况下我需要 .template
- 如何理解"Temporary objs are destroyed as the last step in evaluating the full-expression"?谁能用一些简单的例子来说明这
- template<class T, int N> void h(T const(&)[N]); as friend function
- 为什么我会收到错误"non-template 'f' used as template"
- "unspecialized class template can't be used as a template argument"是什么意思?
- "class template has already been declared as a non-class template"