defining class-object declaration in header gives error

A

Ami

Hi All,
I have a class and i want to share the instance of it, to many
different units. To do this, I have declared the class type object in
common header file, which is included in required units.

e.g. Class_AB *objabc;

When i try to compile program, it gives me 100's of error message. am I
doing wrong? How to share otherwise the object instance in different
program units.

I am using VC++6.0 as programming IDE.

Thanks in advance for suggestions.
 
H

Heinz Ozwirk

Ami said:
Hi All,
I have a class and i want to share the instance of it, to many
different units. To do this, I have declared the class type object in
common header file, which is included in required units.

e.g. Class_AB *objabc;

When i try to compile program, it gives me 100's of error message. am I
doing wrong? How to share otherwise the object instance in different
program units.

Don't define variables in header files. Define them in only one CPP file and
write an extern declaration in the header. Share only the declaration:

// Header.h
extern Class_AB* objabc;


// CPP file
#include "Header.h"
....
Class_AB* objabc;



HTH
Heinz
 
M

Marc BAUDY

Ami a écrit :
Hi All,
I have a class and i want to share the instance of it, to many
different units. To do this, I have declared the class type object in
common header file, which is included in required units.

e.g. Class_AB *objabc;

When i try to compile program, it gives me 100's of error message. am I
doing wrong? How to share otherwise the object instance in different
program units.

I am using VC++6.0 as programming IDE.

Thanks in advance for suggestions.

Define the class type object in source file and in the associated header
in extern like this:

in test.c:
static Class_AB* objabc[ = ...];

in test.h:
extern Class_AB* objabc;

This will work ...

Marco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top