Header issue when combing two existing C++ programs

Soldato
Joined
13 Mar 2011
Posts
7,484
Location
Bada Bing
I have a program that I have written in C++ that compiles and works fine. I am trying to combine this with another C++ program that uses my program as a function.

The layout of my program is as follows:
Mine.png


The layout of their program:
Theirs.png


And what I have done is to put my program in as one of the functions of their program. The combined program has a layout like this:
Together.png


The problem is when I try to compile this, I get a lots of errors:
"x already defined in Old_Main.Obj"

So someone told me about forward declaring the function and NOT including the A2.h in B.Cpp. When I try to compile this with gcc I get:
"undefined reference to x"

Can anybody give me some advice on how to do this please?
 
Last edited:
Soldato
OP
Joined
13 Mar 2011
Posts
7,484
Location
Bada Bing
Been a looong time since I wrote code in C, but the convention was usually to wrap your header files with a #ifndef ... #endif and a unique key e.g.



#ifndef _MY_MAGIC_KEY_FOR_THIS_FILE

#define _MY_MAGIC_KEY_FOR_THIS_FILE 1

.... lots of other stuff

#endif


This then stops the pre-processor including your header file twice.

Yep, i've got all that in my header files, but thanks for reminding me of its importance

Code:
#pragma once

at the top of your header files is the modern way of doing that peter. Every modern compiler recognises it now.

Ive never seen that before, thats interesting

"x already defined in Old_Main.Obj"

means that you're defining x twice. You probably have two classes named the same thing in two separate cpp files.

I solved this issue - it was becuase i included a header file in another header file rather than the cpp file - sorted now though thanks

"undefined reference to x"

This means that x hasn't been defined anywhere, either you forgot to include the cpp in the list of source files to compile or else you never added the definition of x.

This is a puzzling one becuase I have included the header file in the cpp so it can "find" the function, but it stull gives me the error...

I really can't tell much from your diagrams as to the structure of your code, I highly recommend learning some simple UML for diagrams like this. Any decent programmer will be able to read them and instantly know what classes derive from what and what objects own what.

http://www.tutorialspoint.com/uml/uml_class_diagram.htm

Thanks for the tip - i didnt really know how to express myself so its good to know there is a proper way to do it
 
Soldato
OP
Joined
13 Mar 2011
Posts
7,484
Location
Bada Bing
Aye I'm a bit confused still looking at those diagrams....but'#pragma once' is your friend either way :)

I'll have a look into it :)

Well your whole approach sounds a bit kludgey and guess work, but if you can zip all your files and upload somewhere and give a link, I'll try to sort it out for you.

It's impossible to tell what the problem is without the actual files.

Yea this would be useful, I recommend signing up for a free Github account. Not only is it a handy backup of your work but it's also easy for us to check out your code and look through it.

The problem is that you guys would need to install the same library that I am trying to get working - OpenBeagle. All im trying to do is to use one of their examples but make a custom function.
 
Back
Top Bottom