Compiling problems, I don't know what I would make the title. D:

1>stdafx.obj : error LNK2005: "void __cdecl MapRefresh(void)" (?MapRefresh@@YAXXZ) already defined in Program.obj

1>stdafx.obj : error LNK2005: "char (* Map)[11]" (?Map@@3PAY0L@DA) already defined in Program.obj

1>C:\Documents and Settings\CPlusPlus\my documents\visual studio 2010\Projects\Program\Debug\Program.exe : fatal error LNK1169: one or more multiply defined symbols found


This happened when I tried to create a function in a header file instead of putting it into the main .cpp file. The code for the header is

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
//Where most of the games functions are held

#include "stdafx.h"

using namespace std;

char Map[11][11]= {{"+--------+"},
                   {"|        |"},
                   {"|        |"},
                   {"|        |"},
                   {"|        |"},
                   {"|        |"},
                   {"|        |"},
                   {"|        |"},
                   {"|        |"},
                   {"+--------+"}};

void MapRefresh();

void MapRefresh()
{
	int CoordX = 1;
    int CoordY = 1;

	for(int col=0;col<11;col++)
	{

		for(int row=0;row<11;row++)
    {
		if(col==CoordX && row==CoordY)
		{
			cout << "X";
		}
		else
		{
			cout << Map[col][row];
		}
	}
		cout << endl;
    }
}


In the .cpp file, everything works fine except for the part where it calls the function, of course.

1
2
3
4
5
	MapRefresh();

	cin.ignore( 99999999999, '\n' );

	return 0;


There is nothing else that relates the two files. I included the header file to stdafx.h(The file where I include all of my headers), then included that one to the .cpp. Yes I used "" and not <>.
Why do you have this: void MapRefresh(); when you have defined your function right below?
Yeah, I don't really need it. I just did it because... I don't know, habit? Either way, it still presents the same error.
Sounds like you have a stdafx.cpp. Why?
Default comes with a VSE project. Why woul- Oh, thanks! What problem would having that cause? I never really delete it, I know it would clean things up, but I didn't think it would cause errors.
filipe wrote:
Sounds like you have a stdafx.cpp. Why?

It's a MSVC created program and is not the reason for the error

I think the ERROR here is that he said he has put the function definitions in the header file.

Last edited on
But, it did fix it. Are you not supposed to put function definitions in header files? Is that not standard or something?
The compiler will make an object code file (.obj) for every .cpp file in your project. Since your header doesn't seem to have an include guard, it can be #included more than once, which causes multiple definitions problems.

An include guard is this:

1
2
3
4
5
6
#ifndef SOME_UNIQUE_SYMBOL
#define SOME_UNIQUE_SYMBOL

// header code

#endif 

SOME_UNIQUE_SYMBOL can be anything, as long as it's guaranteed to be unique throughout the entire program. Typically it's something like HEADER_FILE_NAME_H or HEADER_FILE_NAME_INCLUDED.
But, it did fix it. Are you not supposed to put function definitions in header files? Is that not standard or something?

Declarations go into header files. Definitions go into .cpp files that #include the required headers.
Last edited on
Okay, well, thanks guys. I'll make sure to this stuff in future programs!
Topic archived. No new replies allowed.