Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Class type redefinition" error between header and source files

So I'm having a problem which I'm sure there is an extremely obvious solution for, but I just can't seem to figure it out. Basically, when I try to do class definitions in my headers and implementation in my source files, I am getting an error saying that I am redefining my classes. Using Visual C++ 2010 Express.

Exact error: "error C2011: 'Node' : 'class' type redefinition"

Example code included below:

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;
    }

}
like image 455
trevwilson Avatar asked Nov 20 '12 23:11

trevwilson


1 Answers

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

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

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

}

you declare the class twice in your code, the second time being in your .cpp file. In order to write the functions for your class you would do the following

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

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

no class is required

like image 57
Syntactic Fructose Avatar answered Sep 23 '22 23:09

Syntactic Fructose