# Why Won't this C++ Code Compile?

**URL:** <https://boards.straightdope.com/t/why-wont-this-c-code-compile/407992>\
**Category:** Factual Questions\
**Created:** [June 14, 2007, 1:47pm UTC](https://boards.straightdope.com/t/why-wont-this-c-code-compile/407992 "2007-06-14T13:47:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fiddle\_Peghead](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/fiddle_peghead/32/3506_2.png) [@Fiddle\_Peghead](https://boards.straightdope.com/u/Fiddle_Peghead)\
**Post date:** [June 14, 2007, 1:47pm UTC](https://boards.straightdope.com/t/why-wont-this-c-code-compile/407992/1 "2007-06-14T13:47:34Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Capt.Ridley\_s\_Shooting\_Party](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@Capt.Ridley\_s\_Shooting\_Party](https://boards.straightdope.com/u/Capt.Ridley_s_Shooting_Party)\
**Post date:** [June 14, 2007, 1:52pm UTC](https://boards.straightdope.com/t/why-wont-this-c-code-compile/407992/2 "2007-06-14T13:52:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Fiddle\_Peghead](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/fiddle_peghead/32/3506_2.png) [@Fiddle\_Peghead](https://boards.straightdope.com/u/Fiddle_Peghead)\
**Post date:** [June 14, 2007, 2:21pm UTC](https://boards.straightdope.com/t/why-wont-this-c-code-compile/407992/3 "2007-06-14T14:21:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fiddle\_Peghead](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/fiddle_peghead/32/3506_2.png) [@Fiddle\_Peghead](https://boards.straightdope.com/u/Fiddle_Peghead)\
**Post date:** [June 14, 2007, 3:01pm UTC](https://boards.straightdope.com/t/why-wont-this-c-code-compile/407992/4 "2007-06-14T15:01:01Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![groman](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groman](https://boards.straightdope.com/u/groman)\
**Post date:** [June 14, 2007, 4:09pm UTC](https://boards.straightdope.com/t/why-wont-this-c-code-compile/407992/5 "2007-06-14T16:09:05Z")

</div>

[QUOTE=Fiddle Peghead]

basic\_string bStr(“bstring”);

cout \<\< bStr \<\< endl;  
[/QUOTE]

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.

---

<div class="post-metadata">

**Author:** ![recessiveMeme](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@recessiveMeme](https://boards.straightdope.com/u/recessiveMeme)\
**Post date:** [June 14, 2007, 5:34pm UTC](https://boards.straightdope.com/t/why-wont-this-c-code-compile/407992/6 "2007-06-14T17:34:40Z")

</div>

[QUOTE=Fiddle Peghead]  
But shouldn’t the following should work?  
[/QUOTE]  
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.

---

<div class="post-metadata">

**Author:** ![Capt.Ridley\_s\_Shooting\_Party](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@Capt.Ridley\_s\_Shooting\_Party](https://boards.straightdope.com/u/Capt.Ridley_s_Shooting_Party)\
**Post date:** [June 14, 2007, 6:32pm UTC](https://boards.straightdope.com/t/why-wont-this-c-code-compile/407992/7 "2007-06-14T18:32:09Z")

</div>

[QUOTE=Fiddle Peghead]  
So I need a constructor to create a CsString out of a basic\_string? If so, what’s the deal with the explicit keyword?  
[/QUOTE]

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