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

    Error - Call to non-static member function...

    I was able to find and resolve most of my errors in this program. But I have 4 bugs left, all the same error message "Call to non-static member function without argument". What am I missing or doing wrong?

    These are the lines effected:
    string getMonthName()
    {
    return monthName[ Date::getMonth() + 1 ];
    } // end of getMonthName function

    void displayNumericDate()
    {
    cout << setfill( '0') << setw( 2 ) << Date::getDay() << "/"
    << setfill( '0') << setw( 2 ) << Date::getMonth()<< "/" << setw( 4 ) << Date::getYear()
    } // end of displayNumericDate function

    void displayLongDate()
    {
    cout << Date::getDay() << " " << getMonthName() << ", " <<Date::getYear()
    } // end of displayLongDate

    Thank you for your assistance

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Error - Call to non-static member function...

    You might want to post the Date class definition. I'm assuming that these function definitions are within the Date class definition, but with what you posted we have to guess as to what really is a member, what isn't; what really is a static member, what isn't.

    Also, please post code in [code][/code] bbcode tags.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Sep 2014
    Posts
    7

    Re: Error - Call to non-static member function...

    Thank you, I was looking for that.

    Code:
    Header file
    
    //
    //  Date.h
    //  Date class with constructor with default arguments
    //  Member functions defined in Date.cpp
    //  C++ Programming
    //  Fall 2014
    //  Paul Peterson
    //
    //
    
    #ifndef ____Date__
    #define ____Date__
    
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    class Date
    {
    public:
    
        // default constructor setting date to January 1, 2000
        explicit Date ( int = 1, int = 1, int=2000 );
        
        // set functions
        // function to set date
        void setDate( int, int, int ); //set date DD/MM/YYYY
        
        // function to set day
        void setDay( int );  //set day
        
        // function to set month
        void setMonth( int ); // set month
        
        // function to set month name
        void setMonthName(string);  // set month name
        
        // function to set year
        void setYear( int); // set year
        
        // get functions
        
        // function to return the day value of the class Date
        int getDay() const; // return day value
    
        // function to return the month value of the class Date
        int getMonth() const; //return month value
        
        // function to get month Name
        string getMonthName() const; // return the name of the month
        
        // function to return the year value of the class Date
        int getYear() const; // return year value
    
        // function to display Date in DD/MM/YYYY format including leading 0 where needed
         void displayNumericDate() const;
       
        // function to display date in long date eg. 1 January, 1900
        void displayLongDate() const;
        
    private:
        
        int month;
        int day;
        int year;
    
    };  // end of class Date
    
    #endif /* defined(____Date__) */
    
    ___________________________________________________
    
    Member Functions:
    
    // Date.cpp
    //
    // C++ Programming
    // Fall 2014
    // Paul Peterson
    
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <array>
    
    using std::cout;
    using std::endl;
    using std::setfill;
    using std::setw;
    using std::string;
    using namespace std;
    
    #include "Date.h"
    
    array < int, 12> numberOfDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    array < string, 12> monthName = { "January", "February", "March", "April", "May",
        "June", "July", "August", "September", "October", "November", "December"};
    
    // Date constructor with arguments and validation
    Date::Date( int day, int month, int year)
    {
        setDate( day, month, year ); //validate and set date
    }  // end Date constructor
    
    // set date value
    void Date::setDate( int d, int m, int y)
    {
        setDay( d ); // get private field day
        setMonth( m ); //get private field month
        setYear( y ); // get private field year
    }  // end function setDate
    
    void Date::setDay ( int d)
    {
        if ( d >= 1 && d <= numberOfDays[ month - 1] )
            day = d;
        else
            day = 1;
    } // end of setDay function
    
    void Date::setMonth (int m)
    {
        if ( m >=1 && m <= 12 )
            month = m;
            else
                month = 1;
                }  // end of setMonth function
    
    void Date::setYear(int y)
    {
        if ( y >= 1900 )
            year = y;
        else
            year = 1900;
    }  // end of setYear function
    
    // return day value
    int Date::getDay() const
    {
        return day;
    } // end of getDay function
    
    // return month value
    int Date::getMonth() const
    {
        return month;
    } // end of getMonth function
    
    // return the month name
    string getMonthName()
    {
        return monthName[ Date::getMonth() + 1 ];
    } //
    
    // return year value
    int Date::getYear() const
    {
        return year;
    } // end of getYear function
    
    // print date in DD/MM/YYYY format
    void displayNumericDate()
    {
        cout << setfill( '0') << setw( 2 ) << Date::getDay() << "/"
        << setfill( '0') << setw( 2 ) << Date::getMonth()<< "/" << setw( 4 ) << Date::getYear()
    }  // end of displayNumericDate function
    
    // print date in long date format eg. 1 January, 1900
    void displayLongDate()
    {
        cout << Date::getDay() << " " << getMonthName() << ", " <<Date::getYear()
    }  // end of displayLongDate

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Error - Call to non-static member function...

    Ah. You forgot to prefix the member function definitions will the class name, e.g.,
    Code:
    string getMonthName()
    {
        return monthName[ Date::getMonth() + 1 ];
    }
    should have been:
    Code:
    string Date::getMonthName()
    {
        return monthName[ Date::getMonth() + 1 ];
    }
    Note that within this member function definition, you don't have to write Date::getMonth() since there is no ambiguity to resolve. You can just write getMonth().
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Error - Call to non-static member function...

    A few other things to note:
    • Change your header inclusion guard macro name too, e.g., use PaulP308_DATE_H instead of ____Date__.
    • Do not use using directives like using namespace std; or using declarations like using std::string at file scope in header files. Rather, fully qualify those names in the header file.
    • Do not #include files after using directives or using declarations. I noticed that you did this in the source file.
    • Your Date class definition only depends on std::string, so you only need to #include <string>. Move #include <iostream> and #include <iomanip> to the source file.
    • numberOfDays and monthName in the source file should be declared const. Furthermore, you should declare them within an unnamed namespace because they are implementation details specific to that source file.
    • If you declare a member function as const in the class definition, then you should declare it as const when you define the member function. An example of such a discrepancy is your displayNumericDate member function.
    • Personally, I would remove the "end of foo function" comments. The indentation should already give a visual indication of where the function starts and ends, and if it doesn't, then the function is probably too long and should be broken up into smaller functions that do one thing and do it well.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Sep 2014
    Posts
    7

    Re: Error - Call to non-static member function...

    Thank you for that. That did clear the initial error code, but now I'm getting the an error stating "Out-of-line definition of 'getMonthName' does not match any declaration in 'Date'.

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Error - Call to non-static member function...

    That is because getMonthName is yet another example of the const member function discrepancy that I mentioned in post #5.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Error - Call to non-static member function...

    numberOfDays and monthName in the source file should be declared const. Furthermore, you should declare them within an unnamed namespace because they are implementation details specific to that source file.
    As numberOfDays and monthName are used only by Date class functions, I would consider having them as private static const members of the date class.

    You can also shorten some of your code. eg
    Code:
    void Date::setMonth (int m)
    {
        if ( m >=1 && m <= 12 )
            month = m;
            else
                month = 1;
                }  // end of setMonth function
    can be replaced by
    Code:
    void Date::setMonth(int m)
    {
         month = (m >=1 && m <= 12) ? m : 1;
    }
    Code:
    return monthName[ Date::getMonth() + 1 ];
    Are you sure this should be +1 ?
    Last edited by 2kaud; September 21st, 2014 at 04:21 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Sep 2014
    Posts
    7

    Re: Error - Call to non-static member function...

    Thank you for both suggestions, and no, that should be Date::getMonth() - 1

  10. #10
    Join Date
    Sep 2014
    Posts
    7

    Re: Error - Call to non-static member function...

    Thank you again.

    Please bear with me, as you can tell, I am a student and a little overwhelmed at the moment, working full time, have a second part time job, a family of 5 who keep me very busy and taking three programming classes this semester. This is the first assignment and I am a little lost with your comments.

    Change your header inclusion guard macro name too, e.g., use PaulP308_DATE_H instead of ____Date__.
    is the actual format of the "PaulP308..." important or can I use something that fits with the course or my name?

    Do not use using directives like using namespace std; or using declarations like using std::string at file scope in header files. Rather, fully qualify those names in the header file.
    sorry for being a bit obtuse, can you dumb that down a little please.

  11. #11
    Join Date
    Sep 2014
    Posts
    7

    Re: Error - Call to non-static member function...

    Do not #include files after using directives or using declarations. I noticed that you did this in the source file.
    talking about the "#include "Date.h"? I should move that up with the other #include statements, right?

    numberOfDays and monthName in the source file should be declared const. Furthermore, you should declare them within an unnamed namespace because they are implementation details specific to that source file.
    Not sure what you mean by "unnamed namespace". I originally had all of the functions declared as const, but was getting error messages saying to delete the const.

    If you declare a member function as const in the class definition, then you should declare it as const when you define the member function. An example of such a discrepancy is your displayNumericDate member function.
    add const to the function prototype in the class file as well as the source file, right?

    As for the end of foo function comments, following the format of the text book. I thought it was a little excessive too, but until I see how the instructor grades I will probably leave them, for now. I thought it was a little much to put an end of function with only one or two lines in the function.

    Please forgive me for asking so many questions today. Just trying to make it through the next week, hopefully things will calm down a little after the race Saturday ("running" my first 1/2 marathon). After Saturday I won't heve quite the sense of urgency to make as much time for running.

  12. #12
    Join Date
    Sep 2014
    Posts
    7

    Re: Error - Call to non-static member function...

    This what you had in mind?

    Code:
    // print date in DD/MM/YYYY format
    void Date::displayNumericDate()
    {
        cout << setfill( '0' ) << setw( 2 ) << getDay() << "/"
        << setfill( '0') << setw( 2 ) << getMonth()<< "/" << setw( 4 ) << getYear();
    }  // end of displayNumericDate function
    
    // print date in long date format eg. 1 January, 1900
    void Date::displayLongDate()
    {
        cout << getDay() << " " << getMonthName() << ", " << getYear();
    }  // end of displayLongDate
    Last edited by PaulP308; September 21st, 2014 at 10:27 PM.

  13. #13
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Error - Call to non-static member function...

    Quote Originally Posted by PaulP308
    is the actual format of the "PaulP308..." important or can I use something that fits with the course or my name?
    It has to be a valid identifier. ____Date__ is a valid identifier, but it is reserved to the implementation (compiler/standard library) since names that begin with an underscore followed by another underscore or an uppercase letter, or names that contain consecutive underscores, are reserved to the implementation. You could just use DATE_H, but since these names must be unique, using a special prefix or having a timestamp suffix are some of the approaches to ensure that the name is unique.

    Quote Originally Posted by PaulP308
    sorry for being a bit obtuse, can you dumb that down a little please.
    In other words, instead of having this in the header file:
    Code:
    using namespace std;
    
    // ...
    
        string getMonthName() const;
    Just write:
    Code:
        std::string getMonthName() const;
    Quote Originally Posted by PaulP308
    talking about the "#include "Date.h"? I should move that up with the other #include statements, right?
    Yes.

    Quote Originally Posted by PaulP308
    add const to the function prototype in the class file as well as the source file, right?
    Yes.

    Quote Originally Posted by PaulP308
    As for the end of foo function comments, following the format of the text book. I thought it was a little excessive too, but until I see how the instructor grades I will probably leave them, for now. I thought it was a little much to put an end of function with only one or two lines in the function.
    I certainly find them excessive. A problem with comments is that they must be updated otherwise they will cause confusion instead of help in understanding the code, but because the compiler and testing tools ignore them, they are less likely to be correctly updated than the code itself. Therefore, it makes sense not to write comments that state things that should be obvious even to a reader who is not expert and/or familiar with the code base (but who knows how to program).

    Quote Originally Posted by PaulP308
    This what you had in mind?
    Close, but you still need the const-ness of the member functions to match what you declared in the class definition.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Error - Call to non-static member function...

    Please bear with me, as you can tell, I am a student and a little overwhelmed at the moment, working full time, have a second part time job, a family of 5 who keep me very busy and taking three programming classes this semester. This is the first assignment and I am a little lost with your comments.
    Welcome! We are a friendly bunch who'll provide advice and guidance. Have you read
    http://forums.codeguru.com/showthrea...ork-assignment

    A comment re your header files. You have statements like
    Code:
    explicit Date ( int = 1, int = 1, int=2000 );
    which whilst valid (only the definition needs to have the parameter names) do very little to inform the user of the header file regarding the purpose of the parameters. I know some textbooks show it like this (and some teachers also show it this way) but IMO I would specify meaningful parameter names in the declaration so that someone looking only at the header gets some idea as for what the parameters are used. eg
    Code:
    explicit Date (int day = 1, int month = 1, int year = 2000);
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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