2
string Point::ToString(const Point& pt)
{
    std::stringstream buffX;  //"Incomplete type is not allowed"????
    buffX << pt.GetX(); // no operator "<<" matches these operands????
    std::stringstream buffY;
    buffY << pt.GetY();

    string temp = "Point(" + buffX + ", " + buffY + ")";  //???....how to combine a couple of strings into one?..
    return temp.str();
}

I followed the code from similar questions, but the system says "Incomplete type is not allowed"---red line under buffX

also red line under "<<" says that---- no operator "<<" matches these operands

really don't know why..

Thank you!

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
Kun
  • 41
  • 1
  • 4

1 Answers1

4

You need to #include <sstream> to use std::ostringstream.

Then:

std::string Point::ToString(const Point& pt)
{
    std::ostringstream temp;
    temp << "Point(" << pt.GetX() << ", " << pt.GetY() << ")"; 
    return temp.str();
}

It's not clear why you're passing in a Point, since this is a member of that class. Perhaps cleaner would be:

std::string Point::ToString() const
{
    std::ostringstream temp;
    temp << "Point(" << GetX() << ", " << GetY() << ")"; 
    return temp.str();
}

This, perhaps incorrectly, assumes that GetX() and GetY() return some kind of numeric type (int, float, double, ...). If this is not the case, you may want to either change them (the principle of least astonishment) or access the underlying data members of the class directly.

If you're struggling with this kind of compiler error, I strongly recommend you get yourself a good C++ book.

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • Thank you so much for you answer! I did include . Now a new problem comes up.. std::string Point::ToString() const { std::ostringstream temp; temp << "Point(" << GetX() << ", " << GetY() << ")"; // red line under temp says this declaration has no storage or type specifier???? return temp.str(); //expect a declaration??? } – Kun Feb 05 '12 at 03:33
  • @Kun: It's hard to make head or tail of that output, but it suggests to me that your `Get?()`s are not actually returning anything. I've updated my answer. – johnsyweb Feb 05 '12 at 04:42
  • 1
    Thanks a lot for your time!! I figured out what was wrong today.. I missed the semi-colon when define the class point... again, thank you! – Kun Feb 05 '12 at 23:56