one or more multiply defined symbols found

I am having a bit of trouble with a simple program that I wrote to practice writing headers. I keep getting this error message:

1>nUnits.obj : error LNK2005: "void __cdecl nTotal(int)" (?nTotal@@YAXH@Z) already defined in nTotal.obj
1>UserNo.obj : error LNK2005: "int __cdecl UserNo(void)" (?UserNo@@YAHXZ) already defined in nUnits.obj
1>c:\users\owner\documents\visual studio 2010\Projects\playing\Debug\playing.exe : fatal error LNK1169: one or more multiply defined symbols found

Clearly I am screwing up the #ifndef header somehow. I can fix the message by changing the# indef NUNITS_H to ifndef nUnits.h but this seems to be a break in tradition, and I end up with this error message instead.


1>c:\users\owner\documents\visual studio 2010\projects\playing\playing\nunits.h(2): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>c:\users\owner\documents\visual studio 2010\projects\playing\playing\nunits.h(3): error C2008: '.' : unexpected in macro definition

I have been experimenting with it and searching the internet for answers for several hours and this community is my last hope for help. The code is as follows:
//Header

#ifndef NUNITS_H // or nUnits.h
#define NUNITS_H // or nUnits.h
void nTotal(int x);
int UserNo();

#endif



//Functions
#include "stdafx.h"
#include <iostream>



int UserNo()
{
using namespace std;
cout << "enter a number to be multiplyed: ";
int x;
cin >> x;
return x;
}

void nTotal(int x)
{
using namespace std;
cout << "the sum of your numbers is: " << x << endl;
}



//Main


#include "stdafx.h"
#include <iostream>



#include "nUnits.h"

int main()
{
using namespace std;
int x = UserNo();
int y = UserNo();
nTotal (x*y);

return 0;

Any help would be greatly appreciated. Hopefully it is something glaring simply to you code wizards out there, and thanks in advance for giving it a look.
Last edited on
You should:
1. include nUnits.h in your nUnits.cpp file. (also include an ifndef guard)
2. Close your main function with a curly bracket (})
3. If you post on this forum it is appreciated if you put your code samples between code tags:
[code]
insert your code sample here
[/code]
Last edited on
closed account (3hM2Nwbp)
Try removing the "comments" after the preprocessor directives. Comments don't work the same when the preprocessor is involved. (See your compiler's warning)
Thanks for taking a look guys.

@ methodos

1. Tryed adding below code to the nUnits.cpp file, with and without ifndef guard, but that only gave me a new set of error messages.
 
#include nUnits.h  

2. My main function (}) was present but I didn't grab it while copy/pasting.
3. I oppologise to you and everyone elce reading this for not properly folowing the sites protocol for posting code.
Thanks for giving my problem a shot!

@Luc Liber

There are not actually any comments in my code, I just included those in the post so that reader whould know where seperate .cpp and .h files began and ended. I guess properly posting my code would have cleared up much of the confusion.
Thanks for giving it a look though!

@ any other reader

I still feel that my problem lies in some mistake in the ifndef header. This is driving me a little crazy lol. Thanks for the help.


closed account (3hM2Nwbp)
Are you including nUnits.h in your precompiled header? If so, you might have a cyclic inclusion going on. From what I can tell, you have 4 files that could be giving you issues. In separate code blocks, could you post:

stdafx.h
nUnits.h
nUnits.cpp
main.cpp
Last edited on
sure.

main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "stdafx.h"

#include <iostream>

#include "nUnits.h"

int main()
{
	using namespace std;
	int x = userNo();
	int y = userNo();
	nTotal (x*y);

	return 0;
}


nUnits.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21


#include "stdafx.h"
#include <iostream>


int userNo()
{	
	using namespace std;
	cout << "enter a number to be multiplyed: ";
	int x;
	cin >> x;
	return x;
}

void nTotal(int x)
{
	using namespace std;
	cout << "the sum of your numbers is: " << x << endl;
}


nUnits.h

1
2
3
4
5
6
7
8

#ifndef NUNITS_H
#define NUNITS_H

void nTotal(int x);
int userNo();

#endif 


Last edited on
closed account (3hM2Nwbp)
nUnits.cpp - add the line #include "nUnits.h" (at the top)
nUnits.h - You need semicolons after the method declarations.
Last edited on
stdafx.h is a prepackaged header with Microsoft Visual c++, i'm not really sure how to get to the code but it's required for pretty much everything as far as I can tell.
Yea, i'm a total no0b. ;)
closed account (3hM2Nwbp)
Precompiled headers help to reduce compilation time in larger projects. Header files that are infrequently changed are #included in the precompiled header (by the user, you). You should see the stdafx.h file in the solution explorer if you want to have a peek inside it.
Last edited on
added the nUnits header to nUnits.cpp
and the semicolons after the method declaration in nUnits.h (i had taken them out right before you asked for a clearer post to see what would happen)

still getting this error message:

1>nUnits.obj : error LNK2005: "void __cdecl nTotal(int)" (?nTotal@@YAXH@Z) already defined in nTotal.obj
1>UserNo.obj : error LNK2005: "int __cdecl userNo(void)" (?userNo@@YAHXZ) already defined in nUnits.obj
1>c:\users\owner\documents\visual studio 2010\Projects\playing\Debug\playing.exe : fatal error LNK1169: one or more multiply defined symbols found
In nUnits.h, you are missing line-ending semi-colons on Lines 5 and 6.

1
2
3
4
5
6
7
8

#ifndef NUNITS_H
#define NUNITS_H

void nTotal(int x);  // need semicolon here
int userNo();  // need semicolon here

#endif 


I am not sure that you have your up-to-date code posted because if you were missing these semicolons, you wouldn't even finish the compile to get to your link stage...

@ kfmfeo4
No, the code that is entered is not current, the semicolons have been inserted in the places that you have designated. I'll edit the post to show the correct code.
Last edited on
The code in your sixth post above should work fine. It works on my side. I too am using Visual Studio, so I can only guess at why it doesn't compile (EDIT: link).
1. It could be that you didn't open your new project as Win32 Console Application.
2. It might be that you mistakenly made an extra .cpp or .h file that defines one or more of the function or something mean like that.

I presume these are both not the case, so I am as confused as you about why this happens. Specifically, it is rather odd that it doesn't work on your side whereas with me it works perfectly. Maybe someone else knows what is going on. Good luck with it.
Last edited on

As methodos mentioned and you confirmed, the problem is in your LINK, not your COMPILE. See if you can get this simpler version to compile and run first.

If you can, then work towards your two .cpp version - in programming, you always want to get something simple working first and then work towards a more complicated set up.

I suspect you are using an IDE - if you were just using a plain compiler on a *nix box, I could show you a simple Makefile to demonstrate how to compile and link properly.

I think the problem lies in how you are telling your IDE what your source files are.

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

int main()
{
	int x = userNo();
	int y = userNo();
	nTotal (x*y);

	return 0;
}


nUnits.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef NUNITS_H
#define NUNITS_H

#include <iostream>

int userNo()
{	
	using namespace std;
	cout << "enter a number to be multiplyed: ";
	int x;
	cin >> x;
	return x;
}

void nTotal(int x)
{
	using namespace std;
	cout << "the sum of your numbers is: " << x << endl;
}
#endif  


Last edited on
@methodos

You give me too much credit lol. I think that the problem is that, I DID have other .cpp files that I was using with preprocessor directives before I wrote the .h file. I removed these files from the workspace in my IDE, but I think that my main() program may still be interacting with them. I am not at home so I can't erase these files and test it, but I am almost certain that your hit the proverbial nail on the head.

Thanks so much, that's a weight off my shoulders.

@kfmfe04

I did indeed write that exact problem first, or rather second. I was making the program progressively more complex as an exercise. The first program was just a main().cpp with a couple of preprocessors on the same program space.

Thanks much for taking such a thorough look at it though, I have learned a lot in the process.

P.S. I will mark this question as solved as soon as I can access my IDE and test it.
Last edited on
Okay, make sure you do a "make clean" or tell your IDE to clean everything and make it from scratch or you might have weird problems due to confused dependencies/left-over builds from previous runs.

Good luck!
@kjmje04

Thanks, I did that after you suggested it. It worked :).

Thanks for the help everyone. This problem is officially solved.
Topic archived. No new replies allowed.