Skip to main content
gamedev.net
🔒 Locked

Multiple Definitions (C++)

Started by Ed C 12 replies 650+ views
Original Post
Ed C
Ed C
In my program i have three files: Main.cpp (contains main()) Russian.h (header file) Russian.cpp (implementation file for Russian.h) Both Main.cpp and Russian.cpp include Russian.h (#include "Russian.h") and Russian.h contains the definition of a global object of class CLib (CLib o_object. This creates an error when linking the files, because o_object is defined twice! How can i stop it being defined twice?
hammerstein_02
hammerstein_02
Does russian.h have anything like

#if!defined RUSSIAN_H
#define RUSSIAN_H

//Code def''s would go in here

#endif



=*=
If things seem bad, think that they can get a whole load worse, and they don''t seem so bad anymore

=*=
sbennett
sbennett
In header file:
extern CLib o_object;

In cpp file:
CLib o_object;
CraZeE
CraZeE
just to summarize the above:

Add in RUSSIAN.H:

#ifndef RUSSIAN_H
#define RUSSIAN_H

extern CLib o_object;
.
.
.

#endif


Add in RUSSIAN.CPP:

...
CLib o_object;
...
To learn, we share... Give some to take some -
Ed C
Ed C
Will that define o_object as a global object, because i want to use it in both Russian.cpp and Main.cpp.
Qw3r7yU10p!
Qw3r7yU10p!
have you taken the answer and tried it?
Ed C
Ed C
Thanks for the help everyone. Well...everyone except petewood. There was no need to speak to me like that.
DerekSaw
DerekSaw
quote:
Original post by Ed C
Thanks for the help everyone. Well...everyone except petewood. There was no need to speak to me like that.

All you need is trying, and a warm happy face. There''s no need to... err... well, you should know. (See also: recursive)
"after many years of singularity, i'm still searching n the event horizon" o
Qw3r7yU10p!
Qw3r7yU10p!
quote:
Ed C: Will that define o_object as a global object, because i want to use it in both Russian.cpp and Main.cpp.


No-one has answered this question yet, but you seem perfectly happy. That''s because you''ve realised that you''ve been given the correct answer. You have tried it and it has worked.

This is what I was suggesting you do. It''s a good way to learn.

Peace
Ed C
Ed C
I asked the question, because i was at school where i do not have a compiler i could try it with!!!

Otherwise i would have tried it myself.
Qw3r7yU10p!
Qw3r7yU10p!
quote:
I asked the question, because i was at school where i do not have a compiler i could try it with!!!


no, you asked the question because you hadn''t taken the time to try it before coming back with another question.
Ed C
Ed C
What is your problem!!!!
SabreMan
SabreMan
quote:
Original post by Ed C
What is your problem!!!!

You could at least try and find an answer to that question yourself before asking. Googlism comes up with a few suggestions: "pete wood is working in chorley", "pete wood is leaving the department to take up a position in australia" and "pete wood is the organiser".

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.