LINK errors when compiling

May 25, 2015 at 5:27pm
I am getting these errors when trying to compile my prodject, I do not think there is anything wrong with my code. Please help

Error 1 error LNK2005: "void __cdecl welcome(void)" (?welcome@@YAXXZ) already defined in functions.obj c:\Users\Dylan\documents\visual studio 2013\Projects\Robot Masters - Text Based\main.obj Robot Masters - Text Based

Error 2 error LNK1169: one or more multiply defined symbols found c:\users\dylan\documents\visual studio 2013\Projects\Robot Masters - Text Based\Debug\Robot Masters - Text Based.exe Robot Masters - Text Based

functions.h
1
2
3
#include <iostream>

void welcome(int number);


functions.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "functions.h"

void welcome()
{
	int number;

	std::cout << "Hello" << std::endl;
	std::cin >> number;

	return;
}


main.cpp
1
2
3
4
5
6
7
#include "functions.cpp"

int main()
{
	welcome();
	return 0;
}

Last edited on May 25, 2015 at 5:28pm
May 25, 2015 at 5:40pm
main.cpp should include functions.h -- not functions.cpp.

As you've included the definition (implementation) of welcome in main.cpp the function will be compiled into both main.obj and functions.obj (hence the linker warning about multiply defined symbols.)

Edit: And as shadowmouse has mentioned (below) the signature of the declaration of welcome() in your header should match that of the implementation in functions.cpp

If you don't fix this you'll get a different linker warning - about an unresolved external - as the linker needs void welcome(); (the function you're using in main) whereas you've implemented void welcom(int number); (a different overload of the function.)

Andy
Last edited on May 25, 2015 at 6:02pm
May 25, 2015 at 5:42pm
The signatures of your functions welcome() are different, they should be the same. You have welcome(int number) and welcome(void). As you're calling welcome() in main.cpp without an input, I presume the one in functions.cpp is correct and you should remove the int number from functions.h. I do question the premise of the function though, you're creating an integer without initialising it to a value and then printing it in a function called welcome, this will probably just come up with a random number, is that what you're trying to do?
May 25, 2015 at 5:53pm
Try this, it may work...

First of all, you need to change your welcome function's prototype in you functions.h (because it doesn't match with what you have in your functions.cpp
so in you functions.h file, replace your line 3 with this:
1
2
3
void welcome(); // you will be asking the user to input the number
                         // you are already declaring int number in your function's body
                        // and so there's no need to pass the number as a parameter 


Then,

In you main.cpp,
instead of including functions.cpp, include the functions.h.
So replace line 1 with this:
#include "functions.h"

Then,
to call the welcome function, you need to create a class object.
So replace line 5 with this:
1
2
3
functions myObject;
myObject.welcome();
return 0;
Last edited on May 25, 2015 at 5:59pm
May 25, 2015 at 5:57pm
Why would you need to create a class object? welcome() isn't a member function.
May 25, 2015 at 6:03pm
what i am trying to do is make it so it displays the word Hello and then the user has to type a number to exit.
May 25, 2015 at 6:04pm
I got it to work guys thanks, I just done what shadowmouse said.
May 25, 2015 at 7:01pm
Did you not also have to fix the #include line in main.cpp, too? To eliminate the "already defined" linkage problem?

Andy
May 25, 2015 at 7:25pm
Yes I did, cheers
Topic archived. No new replies allowed.