defining class-object declaration in header gives error

Discussion in 'C++' started by Ami, Dec 22, 2005.

  1. Ami

    Ami Guest

    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.
     
    Ami, Dec 22, 2005
    #1
    1. Advertising

  2. Ami

    Heinz Ozwirk Guest

    "Ami" <> schrieb im Newsbeitrag
    news:...
    > 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
     
    Heinz Ozwirk, Dec 22, 2005
    #2
    1. Advertising

  3. Ami

    Ami Guest

    Thanks a lot Heinz for your help. It works now.
    Regards
     
    Ami, Dec 22, 2005
    #3
  4. Ami

    Marc BAUDY Guest

    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
     
    Marc BAUDY, Dec 22, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. johny smith
    Replies:
    8
    Views:
    420
    Peter Koch Larsen
    Jul 2, 2004
  2. Ovidesvideo
    Replies:
    4
    Views:
    517
    Andrey Tarasevich
    Dec 10, 2004
  3. divya
    Replies:
    0
    Views:
    928
    divya
    Aug 9, 2007
  4. Andrew
    Replies:
    9
    Views:
    475
    Sherman Pendley
    Dec 7, 2007
  5. mlt
    Replies:
    2
    Views:
    847
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page