Kun
Kun

Reputation: 41

Convert double point coordinates into string

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!

Upvotes: 2

Views: 969

Answers (1)

johnsyweb
johnsyweb

Reputation: 141810

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.

Upvotes: 4

Related Questions