Undefined reference to Widget(double)

I'm attempting to test a FIFO/LIFO sorting program, but I keep getting this error that stumps me. I've tried looking it up, but none of the answers seem to fit my situation.

InventoryManager.h:

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
#if !defined (INVENTORYMANAGER_H)
#define INVENTORYMANAGER_H

#include "StackDeque.h"
#include "QueueDeque.h"
#include "Widget.h"
#include <iostream>
#include <iomanip>
using namespace std;

class InventoryManager
{
	private:
		int inv_choice;
		int buy;
		int sell;
		double profit;
		StackDeque<Widget>* stack;
		QueueDeque<Widget>* queue;
	public:
		InventoryManager(int inventory_choice);	//LIFO	or	FIFO
		~InventoryManager();
		void buyWidgets(double cost, int num_to_buy);
		double getTotalProfit();
		double sellWidgets(double price, int num_to_sell);
};

#endif 


InventoryManager.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
#include "InventoryManager.h"

#include <iostream>
#include <iomanip>

int main()
{
	Widget* num = new Widget(1);
	Widget* widget = new Widget(10);
	InventoryManager* deque = new InventoryManager(1);
	
	double wid = widget->getCost();
	int number = num->getCost();
	
	deque->buyWidgets(wid, number);
	
	delete num;
	delete widget;
	delete deque;
	
	return 0;
}

InventoryManager::InventoryManager(int inventory_choice)
{
	inv_choice = inventory_choice;
	buy = 0;
	sell = 0;
	profit = (sell - buy);
	
	stack = new StackDeque<Widget>();
	queue = new QueueDeque<Widget>();
}

InventoryManager::~InventoryManager()
{
	delete stack;
	delete queue;
}

void InventoryManager::buyWidgets(double cost, int num_to_buy)
{
	Widget* num = new Widget(1);
	int number = num->getCost();
	
	if(inv_choice == 1)
	{
		for(int i = 0; i < number; i++)
		{
			stack->push(num);
			buy = (cost * number);
		}
	}
	else if(inv_choice == 2)
	{
		for(int i = 0; i < number; i++)
		{
			queue->enqueueDeque(num);
			buy = (cost * number);
		}
	}
	
	delete num;
}

double InventoryManager::getTotalProfit()
{
	return profit;
}

double InventoryManager::sellWidgets(double price, int num_to_sell)
{
	Widget* num = new Widget(1);
	int number = num->getCost();
	
	if(inv_choice == 1)
	{
		for(int i = 0; i < number; i++)
		{
			stack->pop();
			sell = (price * number);
		}
	}
	else if(inv_choice == 2)
	{
		for(int i = 0; i < number; i++)
		{
			queue->dequeueDeque();
			sell = (price * number);
		}
	}
	
	cout << getTotalProfit();
	
	return sell;
	
	delete num;
}


Build file:

1
2
3
4
5
6
7
8
9
10
11
@echo off
cls


::set DRIVE_LETTER=%1:
::set PATH=%DRIVE_LETTER%\MinGW\bin;%DRIVE_LETTER%\MinGW\msys\1.0\bin;c:\Windows;c:\Windows\system32

g++ -I.\CSC2110 -c InventoryManager.cpp

g++ -L.\CSC2110 -o  InventoryManager.exe InventoryManager.o Widget.o -lCSC2110
InventoryManager.exe


Error message:


InventoryManager.o:InventoryManager.cpp:(.text+0x2e): undefined reference to `Widget::Widget(double)'
InventoryManager.o:InventoryManager.cpp:(.text+0x52): undefined reference to `Widget::Widget(double)'
InventoryManager.o:InventoryManager.cpp:(.text+0x273): undefined reference to `Widget::Widget(double)'
InventoryManager.o:InventoryManager.cpp:(.text+0x3c9): undefined reference to `Widget::Widget(double)'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: InventoryManager.o: bad reloc address 0x2c in section `.text$_ZN10StackDequeI6WidgetEC1Ev[__ZN10StackDequeI6WidgetEC1Ev]'
collect2.exe: error: ld returned 1 exit status
'InventoryManager.exe' is not recognized as an internal or external command,
operable program or batch file.
Could you please post the code of "Widget.h"?
The problem obviously resides there.
The linker is looking for the object code that implements the following constructor function:

Widget::Widget(double);

The linker cannot find that function. I see that you are linking against Widget.o , so on the assumption that the function should be coming from there, I'm guessing that the function simply doesn't exist.
Widget.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#if !defined (WIDGET_H)
#define WIDGET_H

class Widget
{
	private:
		double cost;
	public:
		Widget(double user);
		virtual ~Widget();
		double getCost();
};

#endif 
Please don't post the same problem multiple times. It gets confusing and wastes peoples time.
See my response here:
http://www.cplusplus.com/forum/beginner/199236/#msg953152

As Moschops said above and as I said in the other thread, you don't have an implementation of:
 
Widget::Widget(double);

Last edited on
My apologies. Thanks.
Topic archived. No new replies allowed.