More LNK Errors

I recently got more LNK errors when compiling my code:

Error 3 error LNK2005: "class Shop s" (?s@@3VShop@@A) already defined in basic.obj C:\Users\Dylan\Documents\Visual Studio 2013\Projects\Robot Masters - Text\main.obj Robot Masters - Text
Error 2 error LNK2005: "class Local l" (?l@@3VLocal@@A) already defined in basic.obj C:\Users\Dylan\Documents\Visual Studio 2013\Projects\Robot Masters - Text\main.obj Robot Masters - Text
Error 4 error LNK2005: "class Local l" (?l@@3VLocal@@A) already defined in basic.obj C:\Users\Dylan\Documents\Visual Studio 2013\Projects\Robot Masters - Text\shop.obj Robot Masters - Text
Error 1 error LNK2005: "class Basic b" (?b@@3VBasic@@A) already defined in basic.obj C:\Users\Dylan\Documents\Visual Studio 2013\Projects\Robot Masters - Text\main.obj Robot Masters - Text
Error 5 error LNK1169: one or more multiply defined symbols found C:\Users\Dylan\Documents\Visual Studio 2013\Projects\Robot Masters - Text\Debug\Robot Masters - Text.exe Robot Masters - Text

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "basic.h"
#include "quest.h"
#include "shop.h"
#include "workshop.h"
#include "local.h"

Basic b;
Local l;
Shop s;

int main()
{

	b.Welcome();
	b.FunctionCaller();
	b.AutoSave();

	std::cout << "" << std::endl; // Testing only
	system("pause"); // Testing only

	return 0;
}


shop.h
1
2
3
4
5
6
7
8
class Shop
{
public:
	void DisplayShop();
	void BuyItem();
	void SellItem();
	void Temp();
};


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

Local l;

void Shop::DisplayShop()
{
	//TODO
}

void Shop::BuyItem()
{
	//TODO
}

void Shop::SellItem()
{
	//TODO
}

void Shop::Temp() //Temporary
{
	l.balance = 1523;
}


basic.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <fstream>
#include <string>

class Basic
{
public:
	void Welcome();
	void FunctionCaller();
	void Help();
	int GetStartBalance();
	void AutoSave();
	void ShowInventory();
};


basic.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
#include "basic.h"
#include "local.h"
#include "shop.h"


Local l;
Basic b;
Shop s;

int Basic::GetStartBalance()
{
	std::ifstream save("save.txt");
	int newBalance;

	save >> newBalance;
	save.close();

	return newBalance;
}

void Basic::Welcome()
{
	l.balance = b.GetStartBalance();
	std::cout << "Welcome to Robot Masters" << std::endl;
	std::cout << "Balance: $" << l.balance << std::endl << std::endl;

	return;
}

void Basic::FunctionCaller()
{
	std::string call;
	bool check = true;

	std::cout << "What function do you want to call?" << std::endl;
	std::cout << "Hint: type 'help' for a list of avalible functions" << std::endl;

	std::cin >> call;
	std::cout << "" << std::endl;

	while (check == true)
	{
		if (call == "help")
		{
			Help();
			check = false;
		}
		else if (call == "shop")
		{
			s.DisplayShop();
			check = false;
		}
		else if (call == "home")
		{
			std::cout << "You are already at the menu" << std::endl;
			std::cout << "What function do you want to call?" << std::endl;
			std::cout << "Hint: type 'help' for a list of avalible functions" << std::endl;
			std::cin >> call;
			std::cout << "" << std::endl;

			check = true;
		}
		else
		{
			std::cout << "That is not a function" << std::endl;
			std::cout << "Hint: type 'help' for a list of avalible functions" << std::endl;
			std::cin >> call;
			std::cout << "" << std::endl;

			check = true;
		}
	}
}

void Basic::Help()
{
	std::ifstream HelpFile("help.txt");
	std::string line;

	while (getline(HelpFile, line))
	{
		std::cout << line << std::endl;
	}

	std::cout << "" << std::endl;
	b.FunctionCaller();

	HelpFile.close();

	return;
}

void Basic::AutoSave()
{
	std::remove("save.txt");
	std::ofstream save("save.txt");
	save << l.balance << std::endl;
	save.close();

	return;
}

void Basic::ShowInventory()
{
	// TODO
}


local.h
1
2
3
4
5
class Local
{
public:
	int balance;
};
If you stop declaring your objects as global and then pass them to where they are needed, this problem will probably stop, as it seems like what you are doing is creating several instantiating the same globals several times in different files. Yet another reason not to use global variables. You also don't seem to understand the purpose of OOP. You don't need to have a pre-existing object in order to define a member function, for example, usually, line 86 of basic.cpp would just be FunctionCaller(); not b.FunctionCaller();
Last edited on
How do i pass them to where i need them instead? I have changed my functions to private and public:

1
2
3
4
5
6
7
8
9
10
11
class Basic
{
public:
	void Welcome();
	void FunctionCaller();
	void ShowInventory();
private:
	void Help();
	int GetStartBalance();
	void AutoSave();
};


1
2
3
4
5
6
7
8
9
10
class Shop
{
public:
	void DisplayShop();
	void Temp();
private:
	void BuyItem();
	void SellItem();
	void Temp();
};
Last edited on
Well, for a start, declare them in main, and remove anything that is like my previous example where a member function is using a specific object. then any time the object is needed, pass it by reference as a function parameter.
Okay so i have done that but how would I reference them as the parameter? What would go inside the ()

(sorry, I don't understand function parameters)
Okay well i should have just looked it up in the first place when you mentioned it, you ment like this right?

1
2
3
4
int main
{
	b.Welcome(l);
}


1
2
3
4
5
class Basic
{
public:
	void Welcome(Local& l);
};


1
2
3
4
5
6
7
8
void Basic::Welcome(Local& l)
{
	l.balance = GetStartBalance();
	std::cout << "Welcome to Robot Masters" << std::endl;
	std::cout << "Balance: $" << l.balance << std::endl << std::endl;

	return;
}
Last edited on
That's the idea for passing l as a function argument.

However from main.cpp, it appears that b, l and s are still global.
b, l and s should be moved inside main.

Yes i changed that like 5 minutes ago when i realised :)

so how would i pass the reference?
Last edited on
Would i do it by:

1
2
3
4
5
6
int main
{
	Basic basic;

	basic.Welcome(local);
}


1
2
3
4
class Basic
{
	void Welcome(Local local);
}


1
2
3
4
5
6
7
8
void Basic::Welcome(Local local)
{
	local.balance = GetStartBalance();
	std::cout << "Welcome to Robot Masters" << std::endl;
	std::cout << "Balance: $" << local.balance << std::endl << std::endl;

	return;
}


I really do not understand this, but can not continue with my project until i do
Last edited on
You had passing by reference correct three posts back.

1
2
3
4
5
class Basic
{
public:
	void Welcome(Local& l);  // the & indicates pass by reference
};


In the most recent post, you removed the &, therefore you're passing by value.
Line 3 in basic.cpp changes a local copy of local, not the caller's instance. Therefore, the change you make at line 3 is lost when Welcome exits.


Okay I have changed it to that, now i get an error that when I call it in main

Error 4 error C2660: 'Basic::Welcome' : function does not take 1 arguments c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\main.cpp 13 1 Robot Masters - Text

main.cpp
1
2
3
4
5
6
7
8
9
10
int main()
{
	Local local;
	Basic basic;
	Shop shop;

	basic.Welcome(local);

	return 0;
}
20 IntelliSense: too few arguments in function call c:\Users\Dylan\Documents\Visual Studio 2013\Projects\Robot Masters - Text\code\basic.cpp 77 17 Robot Masters - Text
Error 9 error C2660: 'Basic::Welcome' : function does not take 1 arguments c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\main.cpp 13 1 Robot Masters - Text
Error 10 error C2660: 'Basic::FunctionCaller' : function does not take 1 arguments c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\main.cpp 14 1 Robot Masters - Text
Error 11 error C2660: 'Basic::AutoSave' : function does not take 1 arguments c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\main.cpp 15 1 Robot Masters - Text
Error 4 error C2511: 'void Shop::Temp(Local &)' : overloaded member function not found in 'Shop' c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\shop.cpp 21 1 Robot Masters - Text
Error 15 error C2511: 'void Basic::Welcome(Local &)' : overloaded member function not found in 'Basic' c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\basic.cpp 17 1 Robot Masters - Text
Error 17 error C2511: 'void Basic::FunctionCaller(Shop &)' : overloaded member function not found in 'Basic' c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\basic.cpp 24 1 Robot Masters - Text
Error 19 error C2511: 'void Basic::AutoSave(Local &)' : overloaded member function not found in 'Basic' c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\basic.cpp 81 1 Robot Masters - Text
Error 18 error C2352: 'Basic::Help' : illegal call of non-static member function c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\basic.cpp 38 1 Robot Masters - Text
Error 16 error C2352: 'Basic::GetStartBalance' : illegal call of non-static member function c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\basic.cpp 18 1 Robot Masters - Text
Error 6 error C2061: syntax error : identifier 'Shop' c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\basic.h 11 1 Robot Masters - Text
Error 13 error C2061: syntax error : identifier 'Shop' c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\basic.h 11 1 Robot Masters - Text
Error 1 error C2061: syntax error : identifier 'Local' c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\shop.h 5 1 Robot Masters - Text
Error 2 error C2061: syntax error : identifier 'Local' c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\basic.h 10 1 Robot Masters - Text
Error 3 error C2061: syntax error : identifier 'Local' c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\basic.h 13 1 Robot Masters - Text
Error 5 error C2061: syntax error : identifier 'Local' c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\basic.h 10 1 Robot Masters - Text
Error 7 error C2061: syntax error : identifier 'Local' c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\basic.h 13 1 Robot Masters - Text
Error 8 error C2061: syntax error : identifier 'Local' c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\shop.h 5 1 Robot Masters - Text
Error 12 error C2061: syntax error : identifier 'Local' c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\basic.h 10 1 Robot Masters - Text
Error 14 error C2061: syntax error : identifier 'Local' c:\users\dylan\documents\visual studio 2013\projects\robot masters - text\code\basic.h 13 1 Robot Masters - Text

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "basic.h"
#include "quest.h"
#include "shop.h"
#include "workshop.h"
#include "local.h"

int main()
{
	Local local;
	Basic basic;
	Shop shop;

	basic.Welcome(local);
	basic.FunctionCaller(shop);
	basic.AutoSave(local);

	std::cout << "" << std::endl; // Testing only
	system("pause"); // Testing only

	return 0;
}


basic.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <fstream>
#include <string>

class Basic
{
	void Help();
	int GetStartBalance();
public:
	void Welcome(Local& local);
	void FunctionCaller(Shop& shop);
	void ShowInventory();
	void AutoSave(Local& local);
};


shop.h
1
2
3
4
5
6
7
8
9
class Shop
{
public:
	void DisplayShop();
	void Temp(Local& local);
private:
	void BuyItem();
	void SellItem();
};


local.h
1
2
3
4
5
class Local
{
public:
	int balance;
};


basic.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
#include "basic.h"
#include "local.h"
#include "shop.h"

int Basic::GetStartBalance()
{
	std::ifstream save("save.txt");
	int newBalance;

	save >> newBalance;
	save.close();

	return newBalance;
}

void Basic::Welcome(Local& local)
{
	local.balance = GetStartBalance();
	std::cout << "Welcome to Robot Masters" << std::endl;
	std::cout << "Balance: $" << local.balance << std::endl << std::endl;
}

void Basic::FunctionCaller(Shop& shop)
{
	std::string call;
	std::string function = "What function do you want to call?";
	std::string help = "Hint: type 'help' for a list of avalible functions";
	bool check = true;

	std::cout << function << std::endl << help << std::endl;;
	std::cin >> call;
	std::cout << "" << std::endl;

	while (check == true)
	{
		if (call == "help")
		{
			Help();
			check = false;
		}
		else if (call == "shop")
		{
			shop.DisplayShop();
			check = false;
		}
		else if (call == "home")
		{
			std::cout << "You are already at the menu" << std::endl;
			std::cout << function << std::endl << help << std::endl;
			std::cin >> call;
			std::cout << "" << std::endl;

			check = true;
		}
		else
		{
			std::cout << function << std::endl << help << std::endl;
			std::cin >> call;
			std::cout << "" << std::endl;

			check = true;
		}
	}
}

void Basic::Help()
{
	std::ifstream HelpFile("help.txt");
	std::string line;

	while (getline(HelpFile, line))
	{
		std::cout << line << std::endl;
	}

	std::cout << "" << std::endl;
	FunctionCaller();
}

void Basic::AutoSave(Local& local)
{
	std::ofstream save("save.txt");
	save << local.balance << std::endl;
}

void Basic::ShowInventory()
{
	// TODO
}


shop.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "shop.h"
#include "basic.h"
#include "local.h"

void Shop::DisplayShop()
{
	//TODO
}

void Shop::BuyItem()
{
	//TODO
}

void Shop::SellItem()
{
	//TODO
}

void Shop::Temp(Local& local)
{
	local.balance = 145;
}


So many errors and I dont know why!
Last edited on
Taking the first few errors:
First error:
20 IntelliSense: too few arguments in function call c:\Users\Dylan\Documents\Visual Studio 2013\Projects\Robot Masters - Text\code\basic.cpp 77

basic.h line 11 says that FunctionCaller takes one argument (shop). basic.cpp line 77, you calling FunctionCaller with no arguments. Since FunctionCaller requires a reference to Shop, you're going to have to pass that in to Help(). You're going to have to fix this at other calls to Help() also (basic.cpp line 38).

basic.h lines 10,11,13: These lines reference Local and Shop which must be defined first, or you can include a forward declaration for them.
4
5
class Local;  // forward declaration
class Shop; // forward declaration 


Similar problem in shop.h line 5. It references Local, but Local has not been declared yet.
A forward declaration here will solve that problem.

After making those changes (and removing workshop and quest headers), it compiles cleanly for me.


Yep that worked, cheers man :) I appreciate it a lot.
Topic archived. No new replies allowed.