Error when using other class' object as argument

This is a discussion on Error when using other class' object as argument within the C++ Programming forums, part of the General Programming Boards category; Hi, Thanks for your replies, people. Each class does use the other, having functions with the other class' objects as ...

  1. #16
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    Hi,

    Thanks for your replies, people.

    Each class does use the other, having functions with the other class' objects as arguments.

    I've read through your answers but I haven't quite understood how the whole forward declaration works. Can someone elaborate a little bit more for me? Thanks.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    I've read through your answers but I haven't quite understood how the whole forward declaration works. Can someone elaborate a little bit more for me? Thanks.
    This would be a forward declaration of B followed by a definition of A where A has a member pointer to B:
    Code:
    class B;
    
    class A
    {
    public:
        // ...
    private:
        B* b;
    };
    The idea is that A does not need the definition of B since it only uses a pointer (or a reference) to B. It also allows one to include A's header in B's header if B's definition depends on A and needs A's definition.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    Thanks, laserlight. That indeed did the trick. I've just declared class B in class A's definition and vice-versa and I was able to compile my application. Thanks again.

    Renan M Z Mendes

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Just remember that it will only work if class A uses a pointer or reference to B.
    Code:
    class B;
    
    class A
    {
    public:
    	// ...
    private:
    	B* pb; // works
    	B& rb; // works
    	B bb; // doesn't work
    };
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    Actually, I'm not using a B (or A) object as a class member. I'm justing using it as an argument in a member function of the other class. But I'll keep that in mind for future situations. Thanks.

    Renan M Z Mendes

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    It's the same for both arguments and member variables, so no worries there.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-13-2008, 04:57 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Inventory tracking of dynamically allocated items
    By Mario F. in forum C++ Programming
    Replies: 11
    Last Post: 07-23-2006, 05:39 PM
  4. Is there a way to tell what class an object is?
    By Loduwijk in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2006, 08:20 PM
  5. C++ Class Object Collection
    By Visual Develope in forum C++ Programming
    Replies: 3
    Last Post: 05-04-2002, 04:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21