![](/img/trans.png)
[英]Template <class T> , "Error: .... does not name a type"
[英]Class 'does not name a type' error in template file
我一直試圖找出我在編譯器中遇到的錯誤原因,並在模板文件“ Node.template”中指出“ node<Obj>
未命名類型”。
我是班級模板的新手,卻四處尋找答案,但仍無法解決此特定問題。
這是兩個文件的代碼:
//Node.h
#ifndef NODE_CAMERON_H
#define NODE_CAMERON_H
#include <string>
using namespace std;
namespace oreilly_A2 {
template <typename Obj>
class node {
public:
typedef std::string value_type;
node(); //constructor for node
node(const value_type& val, Obj* newNext); //constructor with parameters
void set_data(const value_type& new_data); //set the word that this node contains
void set_link(Obj* new_link); //set the 'next' Obj
void set_previous(Obj* new_prev);
value_type data() const; //return this node's word
const Obj* link() const; //return next
const Obj* back() const;
Obj* link(); //return next
Obj* back();
private:
Obj* next; //the next Obj
Obj* previous;
value_type word; //the word this node contains
};
}
#include "Node.template"
#endif
Node.template文件:
//Node.template
template <typename Obj>
node<Obj>::node(const node::value_type& val=value_type(), Obj* newNext=NULL) {
word = val;
next = newNext;
}
template <typename Obj>
node<Obj>::~node() {}
template <typename Obj>
void node<Obj>::set_data(const value_type& new_data){
word = new_data;
}
template <typename Obj>
void node<Obj>::set_link(Obj* new_link){
next = new_link;
}
template <typename Obj>
void node<Obj>::set_previous(Obj* new_prev) {
previous = new_back;
}
template <typename Obj>
value_type node<Obj>::data() const { //return the word
return word;
}
template <typename Obj>
const Obj* node<Obj>::link() const { //return next node (const function)
return next;
}
template <typename Obj>
const Obj* node<Obj>::back() const { //return previous node (const)
return previous;
}
template <typename Obj>
Obj* node<Obj>::link() {
return next; //return next node (non-const)
}
template <typename Obj>
Obj* node<Obj>::back() { //return previous node (const)
return previous;
}
您在名稱空間中聲明了類模板…
…但是在定義其成員函數時忘記了名稱空間。
的確是有沒有類型的命名node<Obj>
僅命名的類型oreilly_A2::node<Obj>
(∀ Obj
)。
您需要在Node.template中使用 namespace oreilly_A2 {
}
。
另外,請停止在頭文件中using namespace std
進行寫入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.