error LNK2001: unresolved external symbol

when i try to build my code (Mine Operation Simulator.cpp) so it can be run it gives me this error;

Line Operation Simulator.obj : error LNK2001: unresolved external symbol "public: static int __cdecl pNode::total_production(class pNode *)" (?total_production@pNode@@SAHPAV1@@Z)

I have no idea what this even means let alone how to fix it
if some one could help me understand this error i would appreciate it.

here is my code

pNode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "pNode.h"

//------------------------------------------constructors--------------------------------//
pNode::pNode()
{
	pNode node;
}

pNode::pNode(string name, int production, int join_hours)
{
	pNode node = pNode();

	node.setName(name);
	node.setProduction(production);
	node.setHours(join_hours);
}

//-----------------------------------------Mutators-------------------------------------------//

void pNode::setName(string new_name)
{
	name = new_name;
}

void pNode::setProduction(int new_production)
{
	production = new_production;
}

void pNode::setHours(int new_hours)
{
	join_hours = new_hours;
}

void pNode::setleft(pNode* new_left)
{
	left_node = new_left;
}

void pNode::setRight(pNode* new_right)
{
	right_node = new_right;
}

void pNode::setParent(pNode* new_parent)
{
	parent = new_parent;
}

void pNode::setCurrent(pNode* new_current)
{
	current = new_current;
}

	//-------------------------------Non-Mutating Member Functions------------------------------------//

string pNode::getName()
{
	return name;
}

int pNode::getProduction()
{
	return production;
}

int pNode::getHours()
{
	return join_hours;
}

pNode* pNode::getleft()
{
	return left_node;
}

pNode* pNode::getRight()
{
	return right_node;
}

pNode* pNode::getParent()
{
	return parent;
}

pNode* pNode::getCurrent()
{
	return current;
}
	//-------------------------------Non-Mutating Static Member Functions------------------------------------//
// If current node has no children return true
// if current node has chaldren return false
static bool isLeaf(pNode* current_node)
{
	if(current_node->getleft() == NULL && current_node->getRight() == NULL)
	{
		return true;
	}
	else
	{
		return false;
	}
}
	
//This function traverses the tree and add up the production of all nodes using recursion
// leaf rule:
// If the current node is a leaf, then return the production

static int total_production(pNode* current_node)
{
	if(isLeaf(current_node))
	{
		return current_node->getProduction();
	}
	else
	{
		return current_node->getProduction() + 
			total_production(current_node->getleft()) + 
			total_production(current_node->getRight());
	}
}

// This is the same as total_production but with join_hours.
static int total_time(pNode* current_node)
{
	if(isLeaf(current_node))
	{
		return current_node->getHours();
	}
	else
	{
		return current_node->getHours() + 
			total_time(current_node->getleft()) + 
			total_time(current_node->getRight());
	}
}


pTree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "pTree.h"	

	//--------------------------------------Constructors----------------------------------------//
	//Default Constructor
	//no Precondition
	//Postcondition: creates an instance of pTree
	pTree::pTree()
	{
	}
	
	//This constructor creates an instance of pTree and also takes a node and stores it as the root
	pTree::pTree(pNode root_node)
	{
		pNode* root_ptr = &root_node;
		setRoot(root_ptr);
	}

	//-------------------------------Non-Mutating Member Functions------------------------------------//

	pNode* pTree::getRoot()
	{
		return root;
	}

	//-------------------------------------Mutators----------------------------------------------------//
	void pTree::setRoot(pNode* new_root)
	{
		root = new_root;
	}


Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "Main.h"

#include <iostream>
using namespace std;

//initialises alpha and production_hours
Main::Main()
{
	alpha = "abcdefghijklmnopqrstuvwxyz";
	int production_hours[2][9] = {{100, 200, 50, 25, 70, 120, 80, 60, 50},
								{10 , 15 , 8 , 2 , 10, 14 , 11, 4 , 9}};
}

void Main::setRoot(pTree tree)
{
	pNode root_node = pNode("Ship", 0,0); //This is the root node
	pNode* root_ptr = &root_node; // pointer to root node created for use in setRoot()

	tree.setRoot(root_ptr);
}

void Main::buildTree(pNode* root)
{
	root->setleft(createMine(0));
	root->setRight(createMine(1));
}

//creates node with values according to i
//Rules:
//If the name is "mine b" only create a right child
//If the name is "mine e" create both left and right children, with diffent values for i
//If the name is "mine c,h,g,f,i then stop creating children! (these are the leaf nodes)
//Always return the pointer to the children as they are stored as they are stored in the parent node
pNode* Main::createMine(int i)
{

	pNode node = pNode("Mine " + alpha[i], production_hours[0][i], production_hours[1][i]);
	
	pNode* ptr = &node;

	if(node.getName() == "Mine b")
	{
		node.setRight(createMine(i+3));
		return ptr;
	}
	else if(node.getName() == "Mine e")
	{
		node.setleft(createMine(i+3));
		node.setRight(createMine(i+4));
		return ptr;
	}
	else if(node.getName() == "Mine c" || node.getName() == "Mine h" ||node.getName() == "Mine g"|| node.getName() == "Mine f" || node.getName() == "Mine i")
	{
		return ptr;
	}
	else
	{
		node.setleft(createMine(i+2));
		node.setRight(createMine(i+3));
	}

	return ptr;
}


Mine Operation Simulator
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "Main.h"

int main()
{
	Main new_main = Main(); //create new instance of main
	pTree Tree;             //instance of pTree is created

	new_main.setRoot(Tree); //sets the root node and stores it to Tree
	new_main.buildTree(Tree.getRoot()); //uses the root node stored in Tree to build the tree

	cout<<pNode::total_production(Tree.getRoot())<<endl;
	return 0;
}


cheers
Your definitions of the static member definitions of pNode are incorrect.

For example with the total_production function on Line 110:
1
2
3
4
static int total_production(pNode* current_node)
{
.....
}

You don't need to repeat the static when actually defining the function, you use the static keyword when declaring the function inside the class, and more importantly:
You still need the class name qualifier when defining the function.
So it should read
int pNode::total_production(pNode* current_node).

same goes for the other static functions.

Last edited on
you use the static keyword when declaring the function inside the class

i dont know what you mean by this


disregard this comment
i worked out what you meant, i was just abit confused is all

Thank you so much for the help
i appreciate it so much

Last edited on
Topic archived. No new replies allowed.