One or more multiply defined symbols found

Jan 5, 2013 at 4:14pm
I get this error when I #include my bitmap class in multiple .cpp files. How can I include it in more than one class without getting this error?
Jan 5, 2013 at 4:17pm
closed account (o3hC5Di1)
Hi there,

You need to use include guards in the header file:

1
2
3
4
#ifndef BITMAPH
#define BITMAPH
//your class declaration here
#endif 


This is recommended for every header file you create.
Hope that helps.

All the best,
NwN
Last edited on Jan 5, 2013 at 6:04pm
Jan 5, 2013 at 4:30pm
I've done that and now everything declared in the header of one of the classes is undefined.

EDIT: Also, in the header of the class that's having the problem, everything between #ifndef and #endif is faded. (I'm using Visual C++ 2010)
Last edited on Jan 5, 2013 at 4:33pm
Jan 5, 2013 at 4:59pm
closed account (o3hC5Di1)
Could you please post the code of that header file?

Thanks,
-N
Jan 5, 2013 at 5:58pm
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
#ifndef BITMAPH
#define BITMAPH
#include "Graphic.h"
#include "Pixel.h"

#pragma once
class Renderer
{
public:
	Renderer(void);
	~Renderer(void);

	void AddGraphic(Graphic*);
	void Prioritize(void);

	Pixel *GetBuffer(void);
	void DrawBuffer(void);
	void DrawBitmap(void);

private:
	void InitBuffer(void);
	void SwapBuffers(void);

	int _graphicssize;
	Graphic *_graphics;

	int _highestpriority;
	int _dlistsize;
	static const int _dlistmax = 10;
	Graphic **_displaylist;

	int _buffersize;
	Pixel *_backbuffer;
	Pixel *_frontbuffer;
};
#endif 
Last edited on Jan 5, 2013 at 5:59pm
Jan 5, 2013 at 6:03pm
closed account (o3hC5Di1)
Sorry, I wasn't clear enough.

The include guard works as follows:

#ifndef BITMAPH - If preprocessor constant BITMAPH was not defined already (first inclusion)
#define BITMAPH - Do define the preprocessor constant, so next time above will be false

So for this to work, every header files needs to define a different preprocessor constant, for your rendere class you might use RENDERERH for instance.

All the best,
NwN
Jan 5, 2013 at 6:56pm
I'm still getting the error. I've added it to every header file in my project and it was already in my bitmap class. The error goes away when I remove one of my bitmap class inclusion. Is there anything else that might cause that?
Jan 5, 2013 at 7:59pm
Insufficient data for meaningful answer.
Jan 5, 2013 at 8:08pm
So what data do you need? (It's a little ironic that you've provided me with insufficient data to provide sufficient data)
Jan 5, 2013 at 8:21pm
minimal code that does reproduce your issue
and the exact error message.

http://www.cplusplus.com/forum/articles/40071/#msg216270
http://www.cplusplus.com/forum/articles/40071/#msg216313


Edit:
> now everything declared in the header of one of the classes is undefined.
I bet to circular includes
you don't need to include those headers, just a forward declaration will suffice.
http://www.cplusplus.com/forum/articles/10627/ (especially point 4)
Last edited on Jan 5, 2013 at 8:25pm
Jan 5, 2013 at 11:39pm
Okay, after reading that last link, it would seem that my problem is that I have two classes (Let's call them class A and class B) which include my bitmap class, and class A includes class B, causing the bitmap class to technically be included twice in class A. Is there some way to get around that? Can I access my bitmap class in class A through class B's inclusion?
Jan 6, 2013 at 12:44am
Nope, that's incorrect.
As long as you are using include guards (because you are using them, ¿right?)
you wouldn't have to worry about multiple inclusion or include order

(as long as you follow the `right way' to avoid circularity)
Jan 6, 2013 at 5:45pm
Yes, every header in my project now has include guards. The problem clearly has something to do with the inclusion of the bitmap class. Everything I've found online says that it's being included twice, but it already had an include guard before I even started having the problem. I'm confused. >.<
Jan 6, 2013 at 6:57pm
Code or didn't happen.
Jan 6, 2013 at 7:57pm
This is the bitmap class I'm using:
http://www.partow.net/programming/bitmap/index.html

All of these have had every line of code removed that didn't fix the error or break the class.
Renderer class header:
1
2
3
4
5
6
7
8
9
10
11
#ifndef RENDERERH
#define RENDERERH

#pragma once
class Renderer
{
public:
	Renderer(void);
	~Renderer(void);
};
#endif 


Renderer class cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "StdAfx.h"
#include "bitmap_image.hpp"
#include "Graphic.h"
#include "Renderer.h"
using namespace std;

Renderer::Renderer(void)
{
	
}

Renderer::~Renderer(void)
{
	
}


Graphic class header:
1
2
3
4
5
6
7
8
9
10
11
#ifndef GRAPHICH
#define GRAPHICH

#pragma once
class Graphic
{
public:
	Graphic(char*,int);
	~Graphic(void);
};
#endif 


Graphic class cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "StdAfx.h"
#include "bitmap_image.hpp"
#include "Graphic.h"

Graphic::Graphic(char *argFilename, int argPriority)
{
	
}

Graphic::~Graphic(void)
{
	
}
Jan 6, 2013 at 10:13pm
The header is bad.
make these functions inline or separate their definition in their own source file.
1
2
3
4
5
checkered_pattern(unsigned int, unsigned int, unsigned char, bitmap_image::color_plane, bitmap_image&)
plasma(bitmap_image&, double const&, double const&, double const&, double const&, double const&, double const&, double const&, double const&, double const&, rgb_store const*)
psnr_region(unsigned int const&, unsigned int const&, unsigned int const&, unsigned int const&, bitmap_image const&, bitmap_image const&)
hierarchical_psnr_r(double const&, double const&, double const&, double const&, bitmap_image const&, bitmap_image&, double const&, rgb_store const*)
hierarchical_psnr(bitmap_image&, bitmap_image&, double, rgb_store const*)


Because you've got a function definition in the header, all the sources that include it will have that function definition.
So the linker finds a lot of definition for the same function (all equal) and gets confused.


The exception are inline and template functions.
Last edited on Jan 6, 2013 at 10:16pm
Jan 7, 2013 at 12:16am
I have no idea how to do that. I'm fairly new to C++.
Jan 7, 2013 at 1:41am
Edit bitmap_image.hpp
Write `inline' in front of those function definitions

By instance,
1
2
3
4
5
6
//line 1480
inline void checkered_pattern(const unsigned int x_width,
                       const unsigned int y_width,
                       const unsigned char value,
                       const bitmap_image::color_plane color,
                             bitmap_image& image)
Jan 7, 2013 at 2:18am
That seems to have fixed it. Thanks for all your help. :)
Topic archived. No new replies allowed.