Home Forums Programming help with codes, can’t spot mistakes

Viewing 10 reply threads
  • Author
    • #7708

      ok here’s the header file everyone
      class Date
      int day;
      int month;
      int year;

      //a const to store the number of months
      static const int nmonth = 12;
      //an array on const int to store the number of days each month
      static const int nday[nmonth];

      //method for checking if it is leap year
      //note the static…means all the objects share this function
      //inside this function you can only use static attributes
      //normal non-static method would be fine either…
      static bool leapyear(int);

      //default constructor
      //constructor with 3 inputs for day, month and year
      //copy constructor not needed

      int getday(){
      return day;

      int getmonth(){
      return month;

      int getyear(){
      return year;

      bool setday(int);
      bool setmonth(int);
      bool setyear(int);

      //operators >,< and == overloaded
      bool operator> (const Date& date2);//date 2 can access the private attribute
      bool operator< (const Date& date2);
      bool operator== (const Date& date2);

      //operator –
      int operator- (const Date& date2);
      //operators ++ and — is useful to determine the result of operator "-"
      //unary operators, so NO Arguments
      //Returning by reference, but can slso return by value

      Date& operator++(int);
      Date& operator–(int);

      //returns a formatted string
      string tostring();

      when i compile, it says this line here has error
      [code:1:5af88a5196]string tostring();[/code:1:5af88a5196]
      error msg: error C2146: syntax error : missing ‘;’ before identifier ‘tostring’

      so…i can’t seem to spot it…help??
      this is an assignment solution that the lecturer sent out.

    • #45686

      #include <string>

      using namespace std;

      or else use

      std::string tostring()

      string is defined in the std namespace which you need to specify in your code.

      Hope that helps..

    • #45687

      hey thanks alot jayd, goes to show i still have a lot to learn huh :)

    • #45689

      using namespace std;

      int main()
      Date date1(23,2,2010);//initialise a date with today’s date
      Date date2;//define a date

      //ask user to input date ie to define date2
      int tmp;
      cout<<"please input your date of birth"<<endl;
      cout<<"year: ";
      cin>> tmp;

      //ask year to user until it is a valid year
      //earlier on setyear()returns a false if user input invalid data

      cout<<"Invalid year"<<endl<<"year:";
      cin>> tmp;

      //ask days to user until it is a valid day
      cout<<"Invalid month"<<endl<<"year:";

      cout<<"Today is:"date1.tostring()<<endl;
      cout<<"you are"<<date1 – date2<<"days old1\n";

      return 0;
      ok i know i probably should figure out this error out myself, but i’ve been stuck for 15 minutes, so please somebody out there, help me!!
      the error msg is:
      error C2146: syntax error : missing ‘;’ before identifier ‘date1’
      i checked around thayt area n it looks fine…
      the error occurs at this line
      [code:1:33a8ff0200]cout<<"Today is:"date1.tostring()<<endl;[/code:1:33a8ff0200]

    • #45690

      You’re missing a << between the "Today is:" string and date1.tostring().

      [code:1:4d6cbf53d7]cout<<"Today is:"<<date1.tostring()<<endl;[/code:1:4d6cbf53d7]

    • #45691


      wow thanks, but why would the msg said i’m missing ";"?? i was paying attention to all the other semicolons instead of the lines…anyway thanks a million!!

    • #45696

      Don’t take compiler error messages too seriously, it’s only the compiler’s interpretation of what was wrong, not what is actually wrong.

      In your case, it saw a string directly followed by a variable name. It was right that something needed to be put between them, but thought what was needed was a ";" rather then a "<<".

    • #45701

      Compilers can’t know what comes next so they choose a message that seems most likely and which would work. You as a programmer have to learn to get familiar with the messages and a history of reasons why that error might show up. It might be worth keeping a text file / database of all the errors you have and the messages you got at the time.

      Usually all you should need to do is check the line number and have a look closely around there in your code for mistakes (related or unrelated to the message). If badly stuck try (sensibly so that the program should still compile) commenting out the line of code and see if the error goes away – that at least tells you are on the correct line.

      Beware that missing ; or } can cause errors to show up in completely unrelated locations in the file as can hidden characters like and unfinished `. Also remember that you can always compile the .c / .cpp file on it’s own – any compilation error will be related to just the file and what it includes – not other unrelated files in the same project.

    • #45702

      Another option is to use Clang as a compiler, it’s not 100% complete yet but it has the best error reporting I’ve seen in a C++ compiler.

    • #45703

      A handy little trick when trying to track down the cause of mysterious compiler messages is to break everything down to simpler statements.

      So instead of this:
      cout<<"Today is:"date1.tostring()<<endl;
      cout<<"you are"<<date1 – date2<<"days old1\n";

      rewrite it like this:
      cout<<"Today is:";
      cout<<"you are";
      cout<<date1 – date2;
      cout<<"days old1\n";

      The compiler will then give you a much more accurate error message.

    • #45710

      I recommend Microsoft Visual C++ 2010 Express

      If your a student – Visual Studio 2010 Professional is available from

      Both are a good way to get ready to tackle DirectX 11 – down the road.

Viewing 10 reply threads
  • The forum ‘Programming’ is closed to new topics and replies.