2014-02-12 21 views
1

錯誤:不能聲明一個類模板沒有名字

"cannot declare a class template with no name"

由鐺給出++具有下列文件,「Foo.cpp中」:

#ifndef foo 
#define foo 

template <class T> 
struct foo { 
private: 
    const T t; 
}; 

#endif 

命令行輸入錯誤:

[email protected]$ clang++ -c foo.cpp 
foo.cpp:5:1: error: cannot declare a class template with no name 
+4

你知道預處理器做什麼?在該文件中的前兩行之後,想象*每*符號'foo'取代爲...什麼被定義爲一次'foo'?哦,是的... *沒有*。 – WhozCraig

+1

你是相當魔術師。 – TemplateRex

回答

4

更改包含警衛。他們定義foo是什麼,與nothng取代的foo任何進一步的發生。這將刪除您的班級模板的名稱。

#ifndef FOO_H_ 
#define FOO_H_ 

// as before 

#endif