-1

first time learning c++. Making a date class were we use integers to represent month, day, and year. We need to have a member function to increment the date to the next day, and free functions to display the date in number and word format. My header file compiles but when I compiled my Date.cpp file in emacs I got these errors:

Date.cpp: In member function âDate Date::operator++()â:
Date.cpp:38: warning: suggest parentheses around â&&â within â||â
Date.cpp:46: error: â(((Date*)this)->Date::yyear ++)â cannot be used as a function
Date.cpp:50: error: conversion from âintâ to non-scalar type âDateâ requested
Date.cpp:51: error: conversion from âintâ to non-scalar type âDateâ requested
Date.cpp:52: error: conversion from âintâ to non-scalar type âDateâ requested
Date.cpp: At global scope:
Date.cpp:58: error: ISO C++ forbids declaration of âdisplay1â with no type
Date.cpp:58: error: no âint Date::display1()â member function declared in class âDateâ
Date.cpp:65: error: ISO C++ forbids declaration of âdisplay2â with no type
Date.cpp:65: error: no âint Date::display2()â member function declared in class âDateâ

The code for my Date.cpp is shown here:

#include "Date.h"
#include <iostream>
#include <cassert>

Date::Date(int month, int day, int year)
{
  mmonth = month;
  dday = day;
  yyear = year;
}

int Date::get_mmonth()const
{ 
  return mmonth;
}
//postcondition: month has been returned

int Date::get_dday()const
{
  return dday;
}
//postcondition: day has been returned

int Date::get_yyear()const
{
  return yyear;
}
//year has been returned

//precondition: day will be incremented 
Date operator ++()
{
  dday++;
  assert(dday >= 1 && dday <= 31);

  mmonth++;
  assert(mmonth >= 1 && mmonth <= 12);

  yyear++;
  if(mmonth == 2 && dday == 28 || dday == 29)
  {
    if(yyear % 4 || yyear % 400)
    {
      std::cout<<"Thats a Leap Year"<<std::endl;
      mmonth++;
      dday++;
      yyear++
      assert(dday >= 1 && dday <= 31);
      assert(mmonth >= 1 && mmonth <= 12);
    }
  }
  return mmonth;
  return dday;
  return yyear;
}

//post condition: date has been incremented

//precondition: Date will  be displayed in number format
Date::display1()
{
  std::cout<<mmonth<<'/'<<dday<<'/'<<yyear<<std::endl;
}
//postcondition: Date is displayed in number format

//precondition: Date will be displayed in word format
Date::display2()
{
  switch(mmonth)
  {
    case 1:
      std::cout<<"Januar"<<std::endl;
      break;

    case 2:
      std::cout<<"February"std::endl;
      break;

    case 3:
     std::cout<<"March"<<std::endl;
     break;

    case 4:
     std::cout<<"April"<<std::endl;
     break;

   case 5:
     std::cout<<"May"<<std::endl;
     break;

   case 6:
     std::cout<<"June"<<std::endl;
     break;

   case 7:
     std::cout<<"July"<<std::endl;
     break;

   case 8:
     std::cout<<"August"<<std::endl;
     break;

   case 9:
     std::cout<<"September"<<std::endl;
     break;

   case 10:
     std::cout<<"October"<<std::endl;
     break;

   case 11:
     std::cout<<"November"<<std::endl;
     break;

   case 12:
     std::cout<<"December"<<std::endl;
     break;

   default;
 }
 std::cout<<mmonth<<'/'<<dday<<'/'<<yyear<<std::endl;
 //post condition: Date will be displayed in word format
}

Why am I getting these errors??

jpkotta
  • 9,237
  • 3
  • 29
  • 34
Iris
  • 29
  • 4
  • 10
  • 1
    You're defining a global `operator++`. I think what you want is `Date Date::operator ++()`. There are a number of things wrong with your `return` statements too though. – Greg Kikola Jan 29 '17 at 00:25
  • I can see why I would need Date Date::operator ++() but what seems to be wrong with my return statements? I thought they were fine when I first coded them – Iris Jan 29 '17 at 00:26
  • 1
    Well they're outside the function for one thing. But you're also returning an `int` instead of a `Date` (the second two `return` statements will never be reached). And `return int mmonth` is also a syntax error because you're trying to declare a variable in the `return` statement. – Greg Kikola Jan 29 '17 at 00:29
  • So to return a Date, would I need to do something like: return mmonth; return dday; etc, ? – Iris Jan 29 '17 at 00:37
  • @RehmaRazzak Remember after the first return is executed the function is done. – drescherjm Jan 29 '17 at 00:46
  • changes have been made! though I seem to have gotten new errors – Iris Jan 29 '17 at 00:55
  • Writing a date class is a great way to learn. If you want to blow away all the other students, and probably the teacher as well, here are so rock solid and very efficient date algorithms you could utilize: http://howardhinnant.github.io/date_algorithms.html – Howard Hinnant Jan 29 '17 at 01:29

1 Answers1

0

There are 2 problems in your code:

  1. in Date operator ++() you are trying to return int 3 times:

    return mmonth;
    return dday;
    return yyear;
    

    while you declared that the function returns Date, and compiler doesn't know how to convert int to Date. Even though the last 2 return statements are not reachable (since the code returns upon reaching first return statement), the compiler issues an error for all three.

    Probably you wanted something like this (in place of those 3 return statements):

    return Date (mmonth, dday, yyear)
    

    On a related note, seeing that all your functions are as a part of Date class, I suspect the operator++ is as well (however I cannot know for certain, due to not seeing the header file, but the compiler output of In member function âDate Date::operator++()â: gives a pretty good clue), so you probably want to rewrite

    Date operator ++()
    

    to

    Date Date::operator ++()
    
  2. All functions in C++ must have return value. If the function doesn't return anything - it must return void.

    So the functions

    Date::display1()
    /*...*/
    Date::display2()
    /*...*/
    

    must be rewritten as

    void Date::display1()
    /*...*/
    void Date::display2()
    /*...*/
    

You would, probably benefit from reading a good C++ book as well, since the errors, in syntax, that you made are so trivial, that a way to go would be covered by a decent book (programming by guessing is not a good way to learn).

Community
  • 1
  • 1
Algirdas Preidžius
  • 1,769
  • 3
  • 14
  • 17