c++ - 类中的字符串//C++

标签 c++ string class function

我是 C++ 的新手,对类中的字符串感到困扰

日期.cpp:

#include "stdafx.h"
#include "Date.h"
#include <sstream>
#include <string>

using namespace std;

Date::Date(int day,int month,int year )
{
    setDate(day,month,year);
}

void Date::setDate(int day,int month,int year)
{
    this->day = day;
    this->month = month;
    this->year = year;
}

string Date::printIt()
{
    std::stringstream res;

    res<<this->day<<"/";
    res<<this->month<<"/";
    res<<this->year;

    return res.str;
}

Date operator+(const Date &date,int day)
{
    Date newDate(date.day,date.month,date.month);

    newDate.day += day;

    if(newDate.day > 30)
    {
        newDate.day%=30;
        newDate.month+=1;

        if(newDate.month>=12)
        {
            newDate.month%=30;
            newDate.year+=1;
        }
    }

    return newDate;
}

日期.h:

#ifndef DATE_H
#define DATE_H 

using namespace std;

class Date
{
private:
    int day,month,year;

    Date(){}

public:
    Date(int day,int month,int year);

    void setDate(int day,int month,int year);
    string printIt();

    friend Date operator+(const Date &date, int day);
};


#endif

问题是 printIt() 函数。 Visual Studio 表示声明不兼容。当我将函数类型更改为 int 时,问题消失了,但为什么 string 有问题?

最佳答案

如果 Date.h 将使用 string 类,则必须在 之前包含必要的头文件Date.h Date.h

关于c++ - 类中的字符串//C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8582286/

相关文章:

c - 在 C 中使用 putchar 和 getchar 删除多个空格

android - 在运行时从另一个应用程序获取自定义 View

asp.net - aspnet 中全局变量的缓存或共享类

c++ - 当基类想要访问继承类的成员时

c++ - QT造物主: How to generate the library and testing executable for a custom widget

c++ - 如何找出迭代器是否在一个范围内?

c++ - std::string和int的串联导致移位。为什么?

javascript - 在 JavaScript 中将 JSON 字符串转换为数组

c++ - 时序算法 : clock() vs time() in C++

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?