Thread: Error when using other class' object as argument

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-13-2008, 05:57 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11: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, 09:20 PM
  5. C++ Class Object Collection
    By Visual Develope in forum C++ Programming
    Replies: 3
    Last Post: 05-04-2002, 04:48 PM