7

だから私は非常に明白な解決策があると確信している問題を抱えていますが、それを理解できないようです. 基本的に、ヘッダーでクラス定義を行い、ソース ファイルで実装しようとすると、クラスを再定義しているというエラーが表示されます。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

ノード.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;
    }

}
4

2 に答える 2

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

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

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

}

コードでクラスを 2 回宣言し、2 回目は .cpp ファイルで宣言します。クラスの関数を作成するには、次のようにします。

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

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

クラスは必要ありません

于 2012-11-20T23:48:46.530 に答える
2

それが言っているように、あなたは Node クラスを再定義しています。.cpp ファイルは、関数を実装するためのものです。

//node.cpp
#include <string>

using namespace std;

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

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

....
于 2012-11-20T23:50:38.047 に答える