当前源代码:

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

};

而在我的主要:
Gregorian date = new Gregorian("June", 5, 1991);
cout << date.twoString();

我收到此错误:
mayan.cc: In function ‘int main(int, char**)’:
mayan.cc:109:51: error: conversion from ‘Gregorian*’ to non-scalar type ‘Gregorian’ requested

有谁知道为什么int到string的转换失败了?我对C++相当陌生,但是对Java熟悉,我花了很多时间来寻找该问题的简单答案,但是目前很困惑。

最佳答案

您正在将Gregorian指针分配给Gregorian。删除new:

Gregorian date("June", 5, 1991);

09-09 23:20