Thread: Error when using other class' object as argument

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    18

    Error when using other class' object as argument

    Hi,

    Let's say I have two classes - A and B. In a public member function of A, I use a B object as argument. I use separate files, so I always include the appropriate .h before the function definition. Even so, my compiler doesn't recognize the objet of 'type B' in my function's declaration... Is there something else I must do for that to work?

    Thanks,

    Renan M Z Mendes

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest program that demonstrates the error.
    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. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Did class B include class A, as well?
    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.

  4. #4
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Maybe this can help you, this should compile with no problems:

    Code:
    // FILE: classA.h
    
    class A{
    
    	public:
    	
    		void test_B(B);
    	
    	private:
    	
    };
    
    void A::test_B(B obj){
    	
    	std::cout << obj.get_letter() << "\n";
    }
    Code:
    // FILE: classB.h
    
    class B{
    
    	public:
    	
    		B(char l) : letter(l) {};
    		
    		char get_letter(){ return letter; };
    	
    	private:
    	
    		char letter;
    };
    Code:
    // FILE: main.cpp
    
    #include <iostream>
    
    #include "classB.h"
    #include "classA.h"
    
    int main(){
    
    	B objB('k');
    	
    	A objA;
    	
    	objA.test_B(objB);
    	
    	return 0;
    }
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note that it's usually a good idea to put descriptive names for the arguments in both declarations, definitions and implementations. Keep them consistent.
    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.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe this can help you, this should compile with no problems:
    Yes, but it is very fragile:
    1. No header inclusion guards.
    2. Lack of forward declaration of B in classA.h so classB.h must be included before classA.h
    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

  7. #7
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by laserlight View Post
    2. Lack of forward declaration of B in classA.h so classB.h must be included before classA.h
    Should you always declare B in A? I usually just keep track of the order of my header files, is this wrong? I always use the inclusion guards (except in my quick replies in the forum, lol!)

    But seriously, should you always have the forward declaration?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by dudeomanodude View Post
    Should you always declare B in A? I usually just keep track of the order of my header files, is this wrong? I always use the inclusion guards (except in my quick replies in the forum, lol!)

    But seriously, should you always have the forward declaration?
    Good thing - when header order is not important
    So yes - I prefer forward declaration when needed

    And includ of other headers, when forward declaration is not enough
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Should you always declare B in A? I usually just keep track of the order of my header files, is this wrong?
    If you want to keep them as separate header files then there should be a forward declaration of B in classA.h so that classA.h can be included on its own. Otherwise it would make sense to just combine the two header files instead of having to keep track of the order of their inclusion.

    EDIT:
    Actually, I am wrong. You do not have a reference or pointer to B in classA.h, so actually classA.h should include classB.h, not use a forward declaration.
    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

  10. #10
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by laserlight View Post
    EDIT:
    Actually, I am wrong. You do not have a reference or pointer to B in classA.h, so actually classA.h should include classB.h, not use a forward declaration.
    9 out of 10 times though you would be right, as one class of mine probably would have a reference or pointer to another class (unlike my little example).

    Would you mind editing my example above with I guess just passing B by reference and showing how you would write the includes?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Would you mind editing my example above with I guess just passing B by reference and showing how you would write the includes?
    I would just write:
    Code:
    #ifndef CLASS_A_H_
    #define CLASS_A_H_
    
    #include "classB.h"
    
    class A{
    
    	public:
    	
    		void test_B(B);
    	
    	private:
    	
    };
    
    void A::test_B(B obj){
    	
    	std::cout << obj.get_letter() << "\n";
    }
    
    #endif
    classB.h is already self contained so it would only need header inclusion guards.

    Of course, I probably would not go about implementing the member functions in the headers, but as you say these are examples.
    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

  12. #12
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Thanks laserlight, so does that mean you wouldn't have to include classB in main then? Or would it not matter thanks to the inclusion guards?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Thanks laserlight, so does that mean you wouldn't have to include classB in main then? Or would it not matter thanks to the inclusion guards?
    Since classA.h indirectly includes classB.h you would not have to include classB.h in main.cpp, but as you noted with header inclusion guards it does not matter so you should just include classB.h since main.cpp uses B objects.
    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

  14. #14
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by laserlight View Post
    Since classA.h indirectly includes classB.h you would not have to include classB.h in main.cpp, but as you noted with header inclusion guards it does not matter so you should just include classB.h since main.cpp uses B objects.
    So if the includes in main were to look like:
    Code:
    #include "classA.h"
    #include "classB.h"
    At which point is classB.o built? When classA.h is read? When it gets to classB.h in main?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Object files are source files, not headers.
    So regardless how many headers you include and where and how, you don't generate an object file.
    It will be generated once the compiler processes the B.cpp.
    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