Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Solving external Object definition 1

Status
Not open for further replies.

Timil

Programmer
May 16, 2001
17
FR
I have quite a problem:

I use a log class called TLog, whith a constructor:
TLog(bool p_view, char* p_insert, char* p_folder, char* p_file_name);
P_view: On screen True, else writen in file
p_insert: char* to be inserted on the beginning on each line.
p_folder: folder where to write the log file
p_file_name: file name.. :p

My problem is:
I want to creat a GameLog and a SysLog like those:
TLog GameLog(false, "GameLog-", "d:\\log\\", "GameLog.log");
TLog SysLog(false, "SysLog-", "d:\\log\\", "SysLog.log");
and call them into a projet with many class, some interdependant and some which aren't..

But the problem is: When I compile the linker annouce that I have multiple definition of the GameLog or SysLog identifier.

I have declared those object in multiple place.
In each .cpp with
external TLog GameLog(false, "GameLog-", "d:\\log\\", "GameLog.log");

or static TLog GameLog(false, "GameLog-", "d:\\log\\", "GameLog.log");

or in the main programme..

But or the GameLog or SysLog can't be reached by the class actually running, or I have the linker error.

So my question is: Of do I declare 2 object definition OUTSIDE a project, then call them at the compilation without any error.. nor multiple definition of those object (not class, just object).

Thank you :)


 
1. You should put all the declaration in a h file and the implementations in cpp file. See following:

in h file

class xxx
{
...variables, private functions
public:
xxx();
xxx(int);
int ontfunc();
...other things
};

in cpp file

xxx::xxx()
{
inplementation here
}
xxx::xxx(int x)
{
....
}
int xxx::eek:therfunc()
{
....
return something;
}


2. A header file must have a standard structure
//hfile.h
#if !defined(__HFILE_H)
#define __HFILE_H
....your declarations here
#endif
this structure of headers prevents multiple declaration. John Fill
1c.bmp


ivfmd@mail.md
 
Hum.. already tried..

The problem is: I need it in different class, and the first class call activate the "#define" clause, preventing others class to use the defined object..

I want a SINGLE instance of my objet GameLog and SysLog, not to redefine them into each class :( :(

Sorry for the questions :D
 
If you want a single instance, is not a problem.
In one place declare the instance:
xxx yourinstance;
in all other places
extern xxx yourinstance;
if constructor takes parameters do it:
xxx yourinstance(parms);
all other
extern xxx yournstance;//without parameters
It is not a problem to do it in a single file in the same place:
xxx yourinstance(parms);
extern xxx yournstance;
extern xxx yournstance;
extern xxx yournstance;
extern xxx yournstance;
extern xxx yournstance;//no problems

John Fill
1c.bmp


ivfmd@mail.md
 
Hum..
What I have not speak about (or maybe I'm not as clear as needed :p): My project have a special aspect.

TProgram call all others class
CClass1 is a class defining one aspect of the game, like the car pilotes will uses
CClass2 is a class defining one aspect of the game, like the car pilotes themselves
CClass3 is a class defining one aspect of the game, like the advancement of researches on the car

So I want to use my TLog instance in each of them..
But if I #include "TLog.h" in each class (TLog containe my TLog GameLog(constructor)) I have multiple definition of GameLog..

I can't understand why :(

Sorry, To be clearer will require to put the whole project on the board :( And I'm not allowed to do this :'(
 
I think you have an OOP problem. Look some implementation sample:
class xxx
{
public:
virtual int DoSomeThing()=0;//it means a pure virtual
//function. Because of it the class is abstract.
};
class tlog
{
public:
void DoSomeThingWith(xxx* a)
{
a->DoSomeThing();
}
};
class xx1:public xxx
{
public:
int DoSomeThing(){do some thing}//virtual function
//implementation
}
class xx2:public xxx
{
public:
int DoSomeThing(){do other thing}
}
class xx3:public xxx
{
public:
int DoSomeThing(){make nothing}
}


int main()
{
xxx* x=new xx1();
tlog y;
y.DoSomeThingWith(x);//will call DoSomeThing from xx1
delete x;//
x=new xx2();
y.DoSomeThingWith(x);//will call DoSomeThing from xx2
delete x;
......
} John Fill
1c.bmp


ivfmd@mail.md
 
OOps :)
Sorry..

Just defined those object in the .cpp containing main :)

Work better this way *LOL*

Timil, sleepy programmer at work :(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top