Trouble with Multiple Files

I have a program that has multiple files. I but all of the libraries I wanted to include in a header file called header.h. I have a color.h file too that I keep getting LNK2005 with. The first .cpp file I have is fine with it but the rest keep saying it was already defined in the first .cpp file, thus giving my that error. The code is below.

header.h
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef _HEADER_H_
#define _HEADER_H_

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "time.h"
#include "color.h"
#include <windows.h>

#endif 

color.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef _COLOR_H_
#define _COLOR_H_

struct colors
{
	static const int RED_WHITE = 0xFC;
	static const int BLUE_WHITE = 0xF9;
	static const int BLACK_WHITE = 0xF0;
	static const int BLACK_BLACK = 0xFF;
	static const int RED_BLACK = 0x0C;
	static const int WHITE_BLACK = 0x0F;
	static const int BLUE_BLACK = 0x09;
	static const int COLOR_BLACK = 0x03;
} color;

#endif 

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

using namespace std;

#include "prototypes.h"


void mainMenu()
{
	clrscr();
	timeHeader();
	cout << endl << setw(47) << "-------------";
	cout << endl << setw(36) << "::";
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color.RED_BLACK);
	cout << "Main Menu";
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color.WHITE_BLACK);
	cout << "::";
	cout << endl << setw(47) << "-------------";
	cout << endl << endl << "\t\t\t\t1. Shop";
	cout << endl << "\t\t\t\t2. Administration";
	cout << endl;
}

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

using namespace std;

#include "prototypes.h"

void main()
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color.WHITE_BLACK);
	mainMenu();
}
what's inside the "prototypes.h"??
Your problem is with color.h

1
2
3
4
struct colors
{
...
} color;


This defines a struct named 'colors' then creates a global object of that struct named 'color'. Basically it's shorthand for this:

1
2
3
4
5
6
struct colors
{
...
};

colors color;


As you should know, you can't put globals in headers this way, as it will result in a multiply defined symbol. Globals are evil anyway.

So yeah... get rid of that "color" bit.

What's more, all the members of this class are static, so you don't need an object of this class. Just access the colors with the scope operator:

1
2
3
int foo = color.RED_WHITE;  // bad

int foo = colors::RED_WHITE;  // good 


Also, main should return an int (shouldn't be void). But that's another issue.
Last edited on
Thank you. That fixed the problem.

Why should main be int and not void. I've heard that but for simplicity I've made it void. No one has ever told me why you shouldn't use a void main function.
Because the C++ standard says so.

It has to do with how programs interact with the OS / calling process. To be honest I don't fully understand it myself - but obeying the standard is enough of a reason for me.
Topic archived. No new replies allowed.