[noob]undefined reference to error

I have been looking up on this one for long, but I couldn't solve it.
What I have been trying to do is to "split" my source files into .h and .cpp files as conventionally done. Another thing I tried doing, as advised by another programmer, is including all the dependencies of a file within itself. The error I am getting is a "Multiple Definition of" type of error. The thing is I've been using the "#ifndef" pre-processor stuff to make sure everything is included only once, yet I am getting the Multiple Definition errors with functions and variables in those files 0_o. I already know I shouldn't include any .cpp inside any of the .h files nor should I define functions there. I have done a lot of research, but still can't seem to figure this one.

I am pretty sure I am doing wrong with the all including stuff; just couldn't figure out where >.<

Here are the project files altogether:
https://www.mediafire.com/?ee9bb8ydwd9w8az

Note: The code compiles fine. The errors occur in the linking part.

Thanks in advance!
Last edited on
you missunderstood what include guards are good for. Read this:

http://en.wikipedia.org/wiki/Include_guard

the do not encompass the #include, they encompass the code inside the header files

None the less: not constant global variables shouldn't be in header files.
Last edited on
Oh thanks! I have been reading this article, http://www.cplusplus.com/forum/articles/10627/ , and now I am working on getting stuff right.


None the less: not constant global variables shouldn't be in header files.

Oh! Is there another place I should place them in then? A cpp perhaps?
But then I can't #include that cpp, right?
Thanks again!

Edit:
Alright! Rearranged everything according to Disch's article. My .h files' content are now the stuff encapsulated with the include guards. Removed all the non-constant variables from globals.h

Now what I am getting is an.. "undefined reference error":
http://i.imgur.com/2F0qk8X.png

You can clearly see I've got those functions defined!!? Now why can't the linker find them?

Edit 2: And is it okay to have initial definitions for the class members in the class declaration itself? I mean something like:
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
//==========AI.h===========
#ifndef AI_INCLUDED
#define AI_INCLUDED

//=================================
// included dependencies
#include "Object.h"
#include <string>
//=================================



class AI_Stuff{
	private:
		int _rate_count=0;
		std::string _key_buffer[6]={"", "", "", "", "", ""};
		void holdKey(int keyindex);
		void tapKey(int keyindex, float rate);
		void releaseKey(int keyindex);
		void updateControls(objcopy * objcop);
		void pushKey(int keyid);
		void clearKeyBuffer();
	public:
		int _current_target=4321;
		/*
		0: Up; 1: Down; 2: Left; 3: Right; 
		4: Attack; 5: Defend; 6: Jump; 7: Special; 
		*/
		bool _keys[8]={false, false, false, false, false, false, false, false};
		
		void setTarget(objcopy * objcop, LOAD * LOADED);
		void playAI(objcopy * objcop, LOAD * LOADED);
		
};
#endif // AI_INCLUDED 
Last edited on
~Bump. Updated with more information and questions. I didn't want to double post, but when I updated, the thread was already on its way to the next page.

Thanks in advance.
But then I can't #include that cpp, right?
Do not include a cpp anyway.


You can clearly see I've got those functions defined!!? Now why can't the linker find them?
I can't see any reason if the file is properly added to the project and hence involved in the build process. So it looks like there's something wrong with your project.

Edit 2: And is it okay to have initial definitions for the class members in the class declaration itself? I mean something like:
C++11 allows it.

if you want to share global variables do that with extern:
x.h:
extern int x;

x.cpp:
int x = 123;

Having a lot of global variables is not a good idea anyway.
Thanks for answering my question! I have already removed all the non-constant variables from that .h file and just shared the values through the functions.

Also, I am positive there isn't a problems with the project. I have received that error on both Code::Blocks and DevCpp. Here is my current source in pastebin (did some cleaning with artistic style):
http://pastebin.com/J5V891u1 --main.cpp
http://pastebin.com/gc8K8Wj5 --Object.cpp
http://pastebin.com/C85xMYj0 --Object.h
http://pastebin.com/amJkk9ba --Interface.cpp
http://pastebin.com/v4uZiQ32 --Interface.h
http://pastebin.com/ccHRDnyy --AI.cpp
http://pastebin.com/R3VXax1R --AI.h
http://pastebin.com/bhWsxw9T --Eval.cpp
http://pastebin.com/ZGHfN0sx --Eval.h
http://pastebin.com/YDF8dsLH --globals.h

Is it possible that the external libraries are not added to your project?
Do you mean the project .h files? Everything I posted above is added to my project: http://prntscr.com/4a293n
Do you mean the project .h files?
No, I mean external libraries like SDL.

Take a look at the tutorials on how to add it to your project:
http://wiki.libsdl.org/Tutorials
I did. The source code was actually working and compiling perfectly before I did this splitting. Just double checked, and I am sure the SDL library is properly installed. It's actually the only external one I am using currently.

Just where am I doing wrong ?_? I did my best to follow Disch article and now this happens.
It might be installed, but is it in your project?

For code::blocks:
Go to the menu 'Project', select 'Porperties...'
A dialog 'Project/targets options' appears. Press the button 'Project's build options...'
A dialog 'Project build options' appears. Select the tab 'Linker settings'. What libraries appear in the list box 'Link libraries:'?
You might need to adjust the 'Search directories'/'Linker' there as well
Yes! You were right! I did have those in the global compiler settings, and I thought that would just be for every project. But once I did that in the Project's options, it compiled and linked perfectly!

On DevCpp the case was different though; it was just the IDE being stupid; I forgot to #include <algorithm> for std::sort in Interface.cpp

Anyway, this was quite weird and very unstable. Now I am facing a problem with C::B, and apparently, it is failing to read a file through fstream. Debugging and watching a string causes a segmentation fault immediately 0_o.
The same code compiles and works with DevCpp however. That iss getting off topic, so I might just start another thread.

Thank you very very much! I appreciate all your time and effort!
Registered users can post here. Sign in or register to post.