2015-10-07 42 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