Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > defining class-object declaration in header gives error

Reply
Thread Tools

defining class-object declaration in header gives error

 
 
Ami
Guest
Posts: n/a
 
      12-22-2005
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.

 
Reply With Quote
 
 
 
 
Heinz Ozwirk
Guest
Posts: n/a
 
      12-22-2005
"Ami" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) oups.com...
> 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


 
Reply With Quote
 
 
 
 
Ami
Guest
Posts: n/a
 
      12-22-2005
Thanks a lot Heinz for your help. It works now.
Regards

 
Reply With Quote
 
Marc BAUDY
Guest
Posts: n/a
 
      12-22-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
[Serializable] attribute gives 'Declaration expected' squiggly Cirene ASP .Net 1 08-19-2008 04:56 AM
Button Inside Header template of Gridview--gives error on click divya ASP .Net 0 08-09-2007 11:04 AM
maxplusII error: a deferred constant declaration without a full declaration is not supported Noah VHDL 5 04-07-2006 02:34 PM
defining or not defining destructors johny smith C++ 8 07-02-2004 08:51 AM



Advertisments