Defining objects - Beginner

Rich
2012-02-15
2012-09-26
  • Rich

    Rich - 2012-02-15

    Have had some trouble defining an object - it compiled initially, but when I
    added the three mutator methods it started giving me the error
    " In member function `void Date::setDay(int)':
    a function-definition is not allowed here before '{' token "

    for each of the three new methods.

    I am working on Dev-C++. The code is

    Header:

    using namespace std;
    
    class Date {
          private:
                  int day, month, year;
          public: 
                Date (); // contructor
    
                void setDay (int); // mutators
                void setMonth (int);
                void setYear (int);
    
                void printFormatted () const;
    
    };
    

    Implementation:

    #include <iostream>
    #include "date.h" // change me
    
    using namespace std;
    
    Date::Date ()
    {
         return;
    }
    
    void Date::setDay (int newDay)
    {
         day = newDay;
         return;
    {
    
    void Date::setMonth (int newMonth)
    {
         month = newMonth;
         return;
    }
    
    void Date::setYear (int newYear)
    {
         year = newYear;
         return;
    }
    
    void Date::printFormatted () const
    {
         cout << day ;
    
         switch (day)
         {
                case 1:
                case 21:
                case 31: cout << "st " ;
                         break;
                case 2:
                case 22: cout << "nd " ;
                         break;
                case 3:
                case 23: cout << "rd " ;
                         break;
                default: cout << "th " ;
         }
    
         cout << month << " " << year << endl ;
         return;
    }
    

    and main body (this is just the default template so far, as I have only tried
    test-compiling it)

    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include "date.h"
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    

    Apologies if it's slap-yourself-in-the-face-blindingly-obvious, but it is
    really frustrating me!

     
  • Rich

    Rich - 2012-02-15

    Sorry, have got my dodgy brace sorted now and it works fine

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks