1

I'm trying to create a Node object using class Node:

int main(){
    for(int i=0; i< 20; i++)
        Node *handle = new Node(i, 10);
}

class Node{
 public:
    static vector<Node> map;
    static int totalNodes;
    vector<Node> connections;   
    int NodeID;

    Node(int ID, int weight){
    NodeID = ID;
    CreateConnections(weight);
    totalNodes++;
    map.push_back(*this);
}

For some reason I get

'Node' : undeclared identifier
'Node' handle : undeclared identifier
 syntax error : identifier node

Moving main() down after the class gives me

unresolved external symbol 
for Node::map and Node::totalNodes

I'm somewhat new to C++ so any tips would be appreciated.

ollo
  • 24,797
  • 14
  • 106
  • 155
Steven Morad
  • 2,511
  • 3
  • 19
  • 25

5 Answers5

5

You have to end your class definition with ;

 class Node
 {
    //....
    map.push_back(*this);
 };  //^^^Cannot miss ;

Meanwhile, you need to put declaration of Node before main.

Another point:

 Node(int ID, int weight){
     NodeID = ID;
     CreateConnections(weight);
     totalNodes++;
     map.push_back(*this);
 }//missing closing } on constructor definition

The reason that you have undefined reference to map and totalNodes is because: static members of class must be initialized outside class. so when you inline constructor tries to access them, they are not defined. so you have undefined reference.

You should do something like the following:

 class Node{
 public:
    static vector<Node> map;
    static int totalNodes;
    vector<Node> connections;   
    int NodeID;
    Node(int ID);
 };


int Node::totalNodes = 0;  //definition of static variables
vector<Node> Node::map;

//^^^define constructor outside class body
Node::Node(int ID){ //your second parameter for ctor not used, so remove it
    NodeID = ID;
    totalNodes++;
    map.push_back(*this);
}
taocp
  • 23,276
  • 10
  • 49
  • 62
  • I think you meant declaration instead of definition. – Tamás Szelei Apr 13 '13 at 18:35
  • Okay, I added `class Node;` before main and added the semicolon at the end of the constructor, now it is giving me the unresolved external error that I listed in my question. Sorry about the closing brace, I didn't paste the member functions and forgot the }; – Steven Morad Apr 13 '13 at 18:36
3

You are declaring Node after its use. Just move the declaration before the main and you should be good:

class Node { ... };
int main() { ... }

You can also do a forward-declaration (declaration for the identifier without the definition) of the class:

class Node;
int main() { ... }
class Node { ... };
Shoe
  • 74,840
  • 36
  • 166
  • 272
1

You need to put Node declaration prior to main where it is being used.
Just putting forward declaration is not enough as new Node in main requires Node to be declared.
As for the unresolved external, you need to define Node::totalNodes

int Node::totalNodes = 0;
alexrider
  • 4,449
  • 1
  • 17
  • 27
1

You either Need to do a Forward-declaration of Node like

class Node;

int main(){
   for(int i=0; i< 20; i++)
   Node *handle = new Node(i, 10);
}

class Node { }; //impelementation

or put main after defining class Node.

bash.d
  • 13,029
  • 3
  • 29
  • 42
  • This won't work as to create new Node with new you need constructor to be declared. – alexrider Apr 13 '13 at 18:32
  • Nonsense! It is a forward declaration, it is just that the class name is known. The compiler will do the rest. – bash.d Apr 13 '13 at 18:34
  • http://liveworkspace.org/code/2wbHmQ$0 source.cpp:8:34: error: invalid use of incomplete type 'class Node' – alexrider Apr 13 '13 at 18:39
  • Are you kidding me? Did you read the comment? I am not pasting the whole code for the sake of readability. – bash.d Apr 13 '13 at 18:45
  • Yes I did read. And I still can't see how forward declaration can help if constructor was involved. Even if you copy whole implementation, it won't help. – alexrider Apr 13 '13 at 18:47
  • I didn't say it was best practice, just somethig to consider! – bash.d Apr 13 '13 at 18:48
  • It not a bad vs good practice, it is question of correct vs incorrect. – alexrider Apr 13 '13 at 18:51
  • At least top answer doesn't suggest such thing. http://stackoverflow.com/questions/553682/when-to-use-forward-declaration – alexrider Apr 13 '13 at 18:57
1

Just bung the following line for the first line:

class Node;
Ed Heal
  • 59,252
  • 17
  • 87
  • 127