2015-10-07 59 views
0

我一直在试图解决我在编译器中遇到的错误的原因,在模板文件“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; 
} 
+1

在定义您使用的是名称空间std时,没有理由让您拥有std :: string。你可以简单地把字符串。我认为在你的头文件中包含名称空间是一种错误的编码习惯,因为可能会与其他命名空间发生冲突。例如,如果您将头部包含在主cpp中,然后决定使用命名空间提升。你的程序会遇到功能问题。如std :: begin()和boost :: begin()。通过在函数之前使用名称空间来删除​​此问题,但如果要这样做,以便您不需要“使用名称空间标准;”。 –

回答

3

你声明的命名空间和hellip里面的类模板;

&hellip;但是当你定义它的成员函数时忘记了命名空间。

确实没有名为node<Obj>的类型,只有名为oreilly_A2::node<Obj>(&forall; Obj)的类型。

您需要namespace oreilly_A2 {}Node.template

此外,请停止在头文件中写入using namespace std