C++ int到字符串转换

Sam*_*eow 5 c++ string int type-conversion

当前源代码:

string itoa(int i)
{
    std::string s;
    std::stringstream out;
    out << i;
    s = out.str();
    return s;
}

class Gregorian
{
    public:
        string month;
        int day;
        int year;    //negative for BC, positive for AD


        // month day, year
        Gregorian(string newmonth, int newday, int newyear)
        {
            month = newmonth;
            day = newday;
            year = newyear;
        }

        string twoString()
        {
            return month + " " + itoa(day) + ", " + itoa(year);
        }

};
Run Code Online (Sandbox Code Playgroud)

在我的主要:

Gregorian date = new Gregorian("June", 5, 1991);
cout << date.twoString();
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

mayan.cc: In function ‘int main(int, char**)’:
mayan.cc:109:51: error: conversion from ‘Gregorian*’ to non-scalar type ‘Gregorian’ requested
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么int到字符串转换失败了?我对C++很新,但熟悉Java,我花了很多时间寻找这个问题的简单答案,但目前我很难过.

jua*_*nza 15

您正在指定一个Gregorian指针Gregorian.放下new:

Gregorian date("June", 5, 1991);
Run Code Online (Sandbox Code Playgroud)