5

Current source code:

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

And in my main:

Gregorian date = new Gregorian("June", 5, 1991); cout << date.twoString(); 

I'm getting this error:

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

Does anyone know why int to string conversion is failing in here? I'm fairly new to C++ but am familiar with Java, I've spent a good deal of time looking for a straightforward answer to this problem but am currently stumped.

3
  • 2
    You can get rid of std::string s; in itoa and just return out.str();. The return string will be constructed before the stringstream is destroyed. Reasonable compilers will likely produce the exact same code in both cases, but the extra temporary tends to suggest to people looking at your code that you don't understand or trust the C++ scoping rules. Commented Apr 23, 2012 at 20:04
  • Unrelated, but did you mean to name the function toString()? Commented Apr 23, 2012 at 20:09
  • Coming from Java, I didn't know if C++ already had a toString() method. I didn't want to overload it if I didn't need to. Commented Apr 23, 2012 at 20:12

2 Answers 2

15

You are assigning a Gregorian pointer to a Gregorian. Drop the new:

Gregorian date("June", 5, 1991); 
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, that was an incredibly fast response. A big thank you for such a simple solution.
@SamMeow no problem. If the answer is OK then you can select it as such.
0

You can use this function to convert int to string, after including sstream:

#include <sstream> string IntToString (int a) { stringstream temp; temp<<a; return temp.str(); } 

1 Comment

This doesn't answer the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.