2012-11-20 13 views
6

非常に明白な解決策があると確信している問題がありますが、わかりません。基本的には、私のヘッダーでクラス定義を行い、ソースファイルで実装しようとすると、クラスを再定義しているというエラーが表示されます。 Visual C++ 2010 Expressを使用します。ヘッダーファイルとソースファイルの間の "クラスタイプの再定義"エラー

正確なエラー: "エラーC2011: 'ノード': 'クラス' 型の再定義":

Node.h:

#ifndef NODE_H 
#define NODE_H 
#include <string> 

class Node{ 
public: 
    Node(); 
    Node* getLC(); 
    Node* getRC(); 
private: 
    Node* leftChild; 
    Node* rightChild; 
}; 

#endif 

Node.cpp:

を以下含ま

例コード

#include "Node.h" 
#include <string> 

using namespace std; 


class Node{ 
    Node::Node(){ 
     leftChild = NULL; 
     rightChild = NULL; 
    } 

    Node* Node::getLC(){ 
     return leftChild; 
    } 

    Node* Node::getRC(){ 
     return rightChild; 
    } 

} 

答えて

7
class Node{ 
    Node::Node(){ 
     leftChild = NULL; 
     rightChild = NULL; 
    } 

    Node* Node::getLC(){ 
     return leftChild; 
    } 

    Node* Node::getRC(){ 
     return rightChild; 
    } 

} 

あなたのデ2回目の.cppファイルにコード内でクラスを2回クラアします。あなたのクラスの関数を作成するためには次の

Node::Node() 
{ 
    //... 
} 

void Node::FunctionName(Type Params) 
{ 
    //... 
} 

を行うだろう何のクラスは、それが言っているようあなたは、Nodeクラスを再定義している

+0

私は質問をした人と同じ問題がありました。しかし、変数はどこに置くのですか(クラスにはプライベートなのですか)? –

2

を必要としません。 .cppファイルは、関数の実装用です。

//node.cpp 
#include <string> 

using namespace std; 

Node::Node() { 
    //defined here 
} 

Node* Node::getLC() { 
    //defined here 
} 

.... 
関連する問題