I'm having trouble returning the string for the month. In the string upDate::getMonthName, the getMonthName is underlined and it says Declaration is incompatible with <error-type> upDate getMonthName().

I don't really understand why it doesn't work. For me, I think should be similar the getMonth, getDay, getYear methods, right?

Also, at the very bottom, I don't really understand the ostream operator. It says that the ostream is not identified? I know how to do other operating overloads like operator++ and operator= but I'm kind of confused on how the ostream one works.

Sorry, I'm just confused about C++ overall :/

#include "upDate.h"
#include <string>
#include <iostream>

using namespace std;

upDate::upDate(){
    month = 5;
    day = 11;
    year = 1959;
}

upDate::upDate(int m, int d, int y) {
    if (((m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 12) && d > 31) ||
        ((m == 4 || m == 6 || m == 9 || m == 11) && d > 30 || (m > 12 || m < 1))) {
        month = 5;
        day = 11;
        year = 1959;
    }
    else if (m == 2 && d >= 29 && y % 4 != 0) {
        month = 5;
        day = 11;
        year = 1959;
    }
    else {
        month = m;
        day = d;
        year = y;
    }
}

void upDate::display() {

    switch (month) {
        case 1:
            cout << "January";
            break;
        case 2:
            cout << "February";
            break;
        case 3:
            cout << "March";
            break;
        case 4:
            cout << "April";
            break;
        case 5:
            cout << "May";
            break;
        case 6:
            cout << "June";
            break;
        case 7:
            cout << "July";
            break;
        case 8:
            cout << "August";
            break;
        case 9:
            cout << "September";
            break;
        case 10:
            cout << "October";
            break;
        case 11:
            cout << "November";
            break;
        case 12:
            cout << "December";
            break;
    }
    cout << " " << day << ", " << year;
}

void upDate::setDate(int m, int d, int y) {
    if (((m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 12) && d > 31) ||
        ((m == 4 || m == 6 || m == 9 || m == 11) && d > 30 || (m > 12 || m < 1))) {
        month = 5;
        day = 11;
        year = 1959;
    }
    else if (m == 2 && d >= 29 && y % 4 != 0) {
        month = 5;
        day = 11;
        year = 1959;
    }
    else {
        month = m;
        day = d;
        year = y;
    }
}
int upDate::getMonth() {
    return month;
}
int upDate::getDay() {
    return day;
}
int upDate::getYear() {
    return year;
}
string upDate::getMonthName(){
    string monthN;
    switch (month) {
        case 1:
            monthN = "January";
            break;
        case 2:
            monthN = "February";
            break;
        case 3:
            monthN = "March";
            break;
        case 4:
            monthN = "April";
            break;
        case 5:
            monthN = "May";
            break;
        case 6:
            monthN = "June";
            break;
        case 7:
            monthN = "July";
            break;
        case 8:
            monthN = "August";
            break;
        case 9:
            monthN = "September";
            break;
        case 10:
            monthN = "October";
            break;
        case 11:
            monthN = "November";
            break;
        case 12:
            monthN = "December";
            break;
    } return monthN;
}

// other methods

ostream &operator<<(ostream &out, upDate &L) {
    out << L.getDay() <<  ", " << L.getYear() << endl; //L.getMonthName() is supposed to         be here too
    return out;
}

and here is upDate.h

#ifndef MYDATE_H_
#define MYDATE_H_

class upDate {

    private:
    int month;
    int day;
    int year;


    public:
    upDate();
    upDate(int, int, int);

   void display();
   void setDate(int, int, int);

   int getMonth();
   int getDay();
   int getYear();
   int convertDateToJulian(int, int, int);
   int convertDateToGregorian(int, int&m, int&d, int&y);
   string getMonthName();

   upDate operator=(upDate);
   upDate operator++();
   upDate operator++(int);
   upDate operator+(int);
   friend upDate operator+(int, upDate);
   upDate operator--();
   upDate operator--(int);
   upDate operator-(int);
   int operator-(upDate);
   friend ostream &operator<<(ostream &out, upDate L);

};

#endif /* MYDATE_H_ */

有帮助吗?

解决方案

I have the feeling this all boils down to simple inclusion problem. I don't see anywhere in your code the inclusion for std::string but regardless, you use it. So in your header add #include <string> after the include guard.

In a similar manner, also include <ostream> (and possibly <iostream>) for the operator << to function properly. Modify string getMonthName(); to be std::string getMonthName(); since you do not want to use an entire namespace in your header file (see "using namespace" in c++ headers).

Then two general comments: getters should be const and operator + can take in a const reference.

And finally the operator << (which you identify as ostream operator) is simply streaming out the data into the given ostream and returns the same ostream in order to be chainable, such as:

std::cout << "Date:" << mydate << std::endl;

Here the compiler will use your operator << to output what you want to see on the stream. Read this http://www.cprogramming.com/tutorial/c++-iostreams.html for more about streaming in c++.

其他提示

Your problem is that you're not placing the #include directives in the header, but in the .cpp file. Put these lines inside the header file upDate.h:

#include <string>
#include <iostream>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top