Error overriding class type between header and source files

So, I have a problem, and I'm sure there is a very obvious solution, but I just can not understand. Basically, when I try to execute class definitions in my headers and implementations in my source files, I get a message that I am redefining my classes. Using Visual C ++ 2010 Express.

Exact error: "Error C2011: 'Node': overriding class type"

Sample code 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; } } 
+6
source share
2 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, a second time in your .cpp file. To write functions for your class, you will do the following

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

no class required

+7
source

You override the Node class, as they say. The .cpp file is for functions only.

 //node.cpp #include <string> using namespace std; Node::Node() { //defined here } Node* Node::getLC() { //defined here } .... 
+2
source

All Articles