Thread: To Call an Object Outside of Class

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    56

    To Call an Object Outside of Class

    Hi...I'm new here. I'm also a beginning Computer Science student, C++ OOP. I have mostly successfully put together a program which contains two objects, firstDate and secondDate. This program accepts two dates, increments the dates by one week, and correctly identifies whether the incremented date is a leap year, what day of the week it is, and what month it is. I am stuck on one little step, however, and that is in trying to get the program to identify which of the two objects converted values are largest in a function definition. If anyone is willing to help me with how to call these values in the function listed below, it would be much appreciated.

    First, I have defined the bool function in my header file, outside of the class:
    Code:
    bool isLarger(int, int);
    Second, I have in my definition file this function definition(which doesn't function), also outside of the class and is the last one in the file:
    Code:
    bool isLarger(int, int)
    {
    	if (firstDate.convert() > secondDate.convert())
    	{
    		cout << "The first date entered is the most recent. " << endl << endl;
    		return true;
    	}
    	else
    	{
    		cout << "The second date entered is the most recent. " << endl << endl;
    		return false;
    	}
    }
    Third, I have no idea how to call it in my application file, my main() function. Would it be something like:
    Code:
    CDate.isLarger(firstDate.convert, secondDate.convert);
    I'm sure I'm just missing something totally obvious, but I appreciate the help. Let me know if I need to attach the entire file(s).

    Many thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of taking two ints, take two Date objects (or whatever type you defined firstDate and secondDate to be) :
    Code:
    bool isLarger(const Date& firstDate, const Date& secondDate)
    {
    	if (firstDate.convert() > secondDate.convert())
    	{
    		cout << "The first date entered is the most recent. " << endl << endl;
    		return true;
    	}
    	else
    	{
    		cout << "The second date entered is the most recent. " << endl << endl;
    		return false;
    	}
    }
    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
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    Thanks for replying so quickly!

    I changed my call to this, and it compiled correctly:
    Code:
    bool isLarger(CDate firstDate, CDate secondDate)
    How do I reference it from the main() function? One of my guesses (and the one with the least errors) was the following, but I received the error message "'CDate' : illegal use of this type as an expression":
    Code:
    isLarger(CDate firstDate, CDate secondDate)
    Thanks again.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You would just pass in the objects:
    Code:
    isLarger(firstDate, secondDate)
    Note that the function signature:
    Code:
    bool isLarger(CDate firstDate, CDate secondDate)
    means that isLarger() will copy the arguments. This can be inefficient, hence my example of passing by const reference.
    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

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    Quote Originally Posted by marQade View Post
    How do I reference it from the main() function?
    Once you have function declared to take arguments by references (as laserlight proposed), you simply call it this way:

    Code:
    CDate firstDate, secondDate;
    
    if ( isLarger(firstDate, secondDate) )
    // do stuff
    Last edited by jimzy; 11-25-2007 at 02:23 PM. Reason: Oh, turns out laserlight already explained this :-)

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    When passing arguments to functions, you pass then names of variables you want to pass, not including their type.
    Then there's a question between by-value and by-const (or by pointer). By value makes copies of every variable you pass (which is what you do in your example). laserlight's example passed by reference - hence the objects aren't duplicated (which also means that function can modify them and the function that called it will also see the changes). There's also const, which implies that the function that takes the arguments shouldn't modify them.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    Thank you!!

    I changed the main call to
    Code:
    isLarger(firstDate, secondDate);
    yet continued to receive an error message for unresolved externals. So I placed the call directly in my header file as well as the definition file and it does compile and run correctly now. I'll show - in my header file:
    Code:
    bool isLarger(CDate firstDate, CDate secondDate);
    and in my definition file:
    Code:
    bool isLarger(CDate firstDate, CDate secondDate)
    {
    	if (firstDate.convert() > secondDate.convert())
    	{
    		cout << "The first date entered is the most recent. " << endl << endl;
    		return true;
    	}
    	else
    	{
    		cout << "The second date entered is the most recent. " << endl << endl;
    		return false;
    	}
    }
    and in my main() function
    Code:
    isLarger(firstDate, secondDate);
    Earlier in this conversation, Laserlight mentioned passing by constant reference. What did you mean by that? How do I make this more efficient?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Read above answer.
    Also note that you should always have an exact copy of your functions in a header. They're called declarations. If they don't match, you can get unresolved external errors.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    Wow Elysia, you replied just as I was sending off my question! Thank you...I'm going to go see if I understand what you were saying, then get back to the thread.

    Thanks again.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    So, would this line be passing by reference rather than by value?
    Code:
    bool isLarger(CDate& firstDate, CDate& secondDate)
    I tried
    Code:
     bool isLarger(const CDate& firstDate, const CDate& secondDate)
    but received errors "cannot convert 'this' pointer from constant CDate to CDate&".

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, the first is correct. You're passing by reference when you add &.
    The second should be correct too. Maybe you should post the code and what line the errors occours on?

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    Quote Originally Posted by marQade View Post
    but received errors "cannot convert 'this' pointer from constant CDate to CDate&".
    If you call isLarger() in CDate class, you might want to dereference 'this' when passing to function, as it is pointer.

    Code:
    isLarger(*this, secondDate)
    instead of

    Code:
    isLarger(this, secondDate)
    ...not sure if this is your problem though.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't think that's the case - it would generate an error that would say something like:
    "Argument - cannot convert from CDate* to CDate&..."
    This error if I'm not mistaken has something to do with const functions, such as calling a non-const member function from a const member function, but I don't know if that's the case either.
    Last edited by Elysia; 11-25-2007 at 03:38 PM.

  14. #14
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    Code:
    bool isLarger(const CDate& firstDate, const CDate& secondDate)
    {
    	if (firstDate.convert() > secondDate.convert())
    	{
    		cout << "The first date entered is the most recent. " << endl << endl;
    		return true;
    	}
    	else
    	{
    		cout << "The second date entered is the most recent. " << endl << endl;
    		return false;
    	}
    }
    The function declaration is identical.
    I receive error message on the header line.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What is CDate, then? There's no such existing class to my knowledge. Is it your own defined class somewhere?
    What's the declaration of this class? This error seems mysterious.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM
  2. 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
  3. Linked list of a class object?....
    By chadsxe in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2005, 03:15 PM
  4. array of class object
    By TomButcher in forum C++ Programming
    Replies: 5
    Last Post: 09-03-2005, 09:48 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM