![](/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.