CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2008
    Posts
    41

    Cannot call another function within class?

    This one is probably a basic oversight...

    First, the code:
    Code:
    //in format.cpp:
    
    void format::setIntLargestValue (int intInput){
            intLargestValue = intInput;
        }
        int format::getIntLargestValue(){
            return(intLargestValue);
        }
        void numberChecker(string strInput){
            if (strInput.length() > format::getIntLargestValue()){
                format.setIntLargestValue(strInput.length());
            }
        }
    setIntLargestValue is just a local proxy. But this is one example of about 10 similar implementations of what I'm trying to do.

    When I compile I get "error: cannot call member function `int format::getIntLargestValue()' without object"
    Now I understand what it's trying to tell me, but is there not a way to call the a function of this class without having to declare an object?
    I'm trying to avoid writing this inside the class:
    Code:
    format clFormat;
    ...
    clFormat.setIntLargestValue(intInput);
    ...
    Any thoughts??
    Last edited by SimbaSpirit; April 5th, 2009 at 09:51 PM. Reason: clarity

  2. #2
    Join Date
    Aug 2007
    Posts
    858

    Re: Cannot call another function within class?

    Now I understand what it's trying to tell me, but is there not a way to call the a function of this class without having to declare an object?
    If you don't give it an object, what exactly is it supposed to operate on? No, it's not possible - only static functions in a class can be called without an object.

    From the example you give my impression is that the numberChecker function should be a member of the format class. Is there some reason it isn't? It's perfectly legal for a member function to call other member functions, because there's already an object present in the form of the this pointer.

  3. #3
    Join Date
    Feb 2009
    Posts
    326

    Re: Cannot call another function within class?

    friend function could be an option

    But I agree with Speedo, makes sense to have it as a member function as it operates exclusively on the class, unless there is a special reason.

  4. #4
    Join Date
    Aug 2007
    Posts
    858

    Re: Cannot call another function within class?

    friend function could be an option
    It will still need a format object to perform its operations on.

  5. #5
    Join Date
    Sep 2008
    Posts
    41

    Re: Cannot call another function within class?

    This is why peer review is such an amazing thing.
    I have to sync my project between two computer and somehow the edition where it read format::numberChecker got dropped.

    This was one of those cases where you read the code too many times and start seeing things that aren't there xD

    Thanks!

    (For anyone who comes across this article in the future, the problem was the function did not belong to the class because the "format::" was left out)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured