C ++运算符重载<<并返回字符串getMonthName方法

时间:2014-03-25 07:19:20

标签: c++ operator-overloading overloading

我在本月返回字符串时遇到问题。在字符串upDate::getMonthName中,getMonthName带有下划线,并显示Declaration is incompatible with <error-type> upDate getMonthName().

我真的不明白为什么它不起作用。对我来说,我认为应该与getMonthgetDaygetYear方法相似,对吗?

另外,在最底层,我并不真正了解ostream运营商。它说ostream没有被识别出来?我知道如何执行其他操作重载,例如operator++operator=,但我对ostream的工作原理感到困惑。

抱歉,我对C ++整体感到困惑:/

#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;
}

这里是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_ * /

2 个答案:

答案 0 :(得分:1)

我觉得这一切归结为简单的包含问题。我在代码中的任何地方都没有看到std::string的包含,但无论如何,您都使用它。因此,在标题中添加#include <string>包含后卫。

以类似的方式,还包括<ostream>(可能<iostream>),以使operator <<正常运行。将string getMonthName();修改为std::string getMonthName();,因为您不想在头文件中使用整个命名空间(请参阅"using namespace" in c++ headers)。

然后是两个一般性评论:getter应该是constoperator +可以接受const引用。

最后,operator <<(您将其标识为ostream运算符)只是将数据流出到给定的ostream中并返回相同的ostream以便可链接,例如:

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

此处编译器将使用您的operator <<输出您想要在流上看到的内容。有关c ++中流式传输的更多信息,请阅读此http://www.cprogramming.com/tutorial/c++-iostreams.html

答案 1 :(得分:0)

您的问题是您没有在标头中放置 #include 指令,而是放在.cpp文件中。将这些行放在头文件 upDate.h

#include <string>
#include <iostream>