简体   繁体   中英

How do you write your class's data to wostringstream in C++?

Say I have a class B which includes an array of class A, I want to use std::wostringstream to output the data in class B. I have overloaded the operator << for Class B, but I just get 'E0349' and 'C2679' error.

Definition of class A

class A
{
public:
    A();
    ~A();
    inline A(float _x, float _y, float _z) :
        x(_x), y(_y), z(_z)
    {
    }
    inline void Set(float _x, float _y, float _z);

    float x, y, z;
    friend std::ostream& operator<<(std::ostream& out, const A& source);
    friend std::wostringstream& operator<<(std::wostringstream& out, const A& source);
private:

};

Definition of class B

class B
{
public:
    A* ArrayOfA;
    bool Initialize(const A* samples,
        unsigned int count);
    friend std::wostringstream& operator<<(std::wostringstream& out, const B& source);

    B();
    ~B();

private:

};

As you can see,class B have an array of class A. I have overloaded the operator << for class A.

std::wostringstream&
operator<<(std::wostringstream& out, const A& source)
{
    out << '<' << source.x << ',' << source.y << ',' << source.z << '>';

    return out;
}

Now, I want to use Class B like:

std::wostringstream wss;
wss << ClassB

But I can not.

Here is the error code, I have overloaded operator << for Class B

std::wostringstream&
operator<<(std::wostringstream& out, const B& source)
{

    for (unsigned int i = 0; i < 4; ++i)
    {
        out << ":" << source.ArrayOfA[i] << std::endl;
        // ERROR:E0349 no operator "<<" matches these operands
        // ERROR:C2679 binary '<<' : no operator found which takes a right-hand operand of type'A' (or there is no acceptable conversion)
    }

    return out;

}

Here is the full code. It's online compiler.

A little bit of long code sample, but with details

The full code is in the URL.

What is wrong? How would you send the ArrayOfA to std::wostringstream ? If you just only use std::ostream, how would you get the string content like std::wostringstream? Is there anything wrong with my operator overloading?

Keep in mind that std::wostringstream << char (or wchar_t ) returns a std::wostream and not a std::wostringstream . So, effectively,

out << '<' << source.ArrayOfA[i];

Would be looking for the function

std::wostream &operator<<( std::wostream &out, const A & );

What you really want, is to take in, and return an std::wostream .

std::wostream&
operator<<(std::wostream& out, const A& source)
{
    out << '<' << source.x << ',' << source.y << ',' << source.z << '>';
    return out;
}

std::wostream&
operator<<(std::wostream& out, const B& source)
{
    for (unsigned int i = 0; i < 4; ++i)
    {
        out << L":" << source.ArrayOfA[i] << std::endl; 
    }
    return out;
}

std::wostream is compatible with std::wostringstream , so something like this will still work:

std::wostringstream wss;
wss << ClassB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM