Why Won't this C++ Code Compile?

I have a class declared like this:

template <unsigned long SIZE>
class CsString
{
.
.
.
explicit CsString(const char* pStr);
.
.

friend bool operator==(const CsString<SIZE>& lhs, const basic_string<char>& rhs ) { return (strcmp(lhs.m_pBuf,rhs.c_str()) == 0);}
.
.
.
}

It is called with this code fragment:

        basic_string&lt;char&gt; bStr("bstring");
        CsString&lt;10&gt; csStr("cstring");

        if (bStr == csStr)
                    cout &lt;&lt; "strings are equal" &lt;&lt; endl;
        else 
                    cout &lt;&lt; "strings are not equal" &lt;&lt; endl;

I get this error message:

m:\2t8lb_commonobjects_r3.2_dev\cade_core\csobjects\src est_bstring.cpp(16) : error C2678: binary ‘==’ : no operator defined which takes a left-hand operand of type ‘class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >’ (or there is no acceptable conversion)

Because your comparison operator takes a CsString on the left and a basic_string on the right, yet you use it the other way around.

So I need a constructor to create a CsString out of a basic_string? If so, what’s the deal with the explicit keyword?

But shouldn’t the following should work?

//=============================================================================
template <unsigned long SIZE>
CsString<SIZE>& CsString<SIZE>:: operator<<(const basic_string<char>& rh)
{
m_fnAppend(rh.c_str());
return *this;
}
Called with:

basic_string bStr(“bstring”);

cout << bStr << endl;
Error:

M:\2t8lb_CommonObjects_R3.2_Dev\CADE_CORE\CsObjects\src est_bstring.cpp(11) : error C2679: binary ‘<<’ : no operator defined which takes a right-hand operand of type ‘class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >’ (or there is no acceptable conversion)

It’s 1am here and I’m up to 56 hours of work this week already, so I’m a little groggy but do you mean:

basic_string<char> bStr(“bstring”);?

I don’t think C++ allows implicit templates based on instantiation like that.

I’ll try to come back later when I have a chance, but first of all, if you want to overload the insertion operator and chain it like this:
cout << bStr << endl;

You need it to return on ostream reference. A super-vague prototype looks like this:
ostream& operator<<(ostream &stream, someObjectToOut o);

…I’ll come back when I can.

You only need to provide an overloaded == operator which takes the two string versions in the correct order.