0

Hi, I'm new to C++ and I'm having trouble with this dang linker error. The full error is as follows:

Error 4 error LNK2001: unresolved external symbol "public: static class std::list<class Node,class std::allocator<class Node> > NodeManager::node_list" (?node_list@NodeManager@@2V?$list@VNode@@V?$allocator@VNode@@@std@@@std@@A) Nodes.obj

Error 5 fatal error LNK1120: 1 unresolved externals

From this error, I have deduced that the problem is with NodeManager::node_list. After doing a little researching, I have found that LNK2001 refers to an undefined object, right? I do define node_list as a static member of NodeManager, so I'm not quite sure what's wrong.

Here's some code (I cut it short for brevity's sake):

Node.h

#ifndef NODES_H
#define NODES_H

#include <vector>
#include <list>

class Node {
public:
	std::vector<int> children;

	Node(int num, float x, float y, float z) {
		num_ = num;
		x_ = x;
		y_ = y;
		z_ = z;
	}

private:
	int num_;
	float x_;
	float y_;
	float z_;
};

class NodeManager {
public:
	static std::list<Node> node_list;
};

void generateNodes();

#endif

Node.cpp

#include "Nodes.h"

#include <list>

void generateNodes() {
	Node n1(1, 346.0, 26.5, -470.0);
	n1.children.push_back(2);
	n1.children.push_back(4);
	n1.children.push_back(5);
	NodeManager::node_list.push_back(n1);

	// defining more nodes
}

The only reason I created a NodeManager class is because I need a list to keep track of my node list, and I was told that you cannot create, for example, a list of Node's within the Node definition. Is this correct?

Any ideas? Also, feel free to provide constructive criticism.

Thanks in advance.

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by SoulMazer
0

static class objects also have to be declared globally in a *.cpp file

// Node.cpp
#include "Nodes.h"

#include <list>

// declare the static object globally
std::list<Node> NodeManager::node_list;

void generateNodes() {
	Node n1(1, 346.0, 26.5, -470.0);
	n1.children.push_back(2);
	n1.children.push_back(4);
	n1.children.push_back(5);
	NodeManager::node_list.push_back(n1);

	// defining more nodes
}

Edited by Ancient Dragon: n/a

0

Wow, thanks for the quick reply. And thanks, that got rid of my errors.

However, is it okay to declare a global list like that if many other files are going to be #include'ing Node.h? Wouldn't that reset its value? That's the reason I was originally trying to do a static list.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.