Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > string and const char

Reply
Thread Tools

string and const char

 
 
Mike
Guest
Posts: n/a
 
      07-27-2010
Hi
I've compiled this on Linux:

string vlstr = str+"sounds/rain.wav"; //str is a string as well

but with VC2008 I get:
-----
error C2784: 'std:: _String_iterator <_Elem,_Traits,_Alloc> std::
operator + (_String_iterator <_Elem,_Traits,_Alloc>::
difference_type , std:: _String_iterator <_Elem,_Traits,_Alloc>) ":
template argument for 'std:: _String_iterator <_Elem,_Traits,_Alloc>"
could not be derived from' const char [16].
-------
Many thanks
 
Reply With Quote
 
 
 
 
Saeed Amrollahi
Guest
Posts: n/a
 
      07-27-2010
On Jul 27, 9:20*pm, Mike <(E-Mail Removed)> wrote:
> Hi
> I've compiled this on Linux:
>
> string vlstr = str+"sounds/rain.wav"; *//str is a string as well
>
> but with VC2008 I get:
> -----
> error C2784: 'std:: _String_iterator <_Elem,_Traits,_Alloc> std::
> operator + (_String_iterator <_Elem,_Traits,_Alloc>::
> difference_type , std:: _String_iterator <_Elem,_Traits,_Alloc>) ":
> template argument for 'std:: _String_iterator <_Elem,_Traits,_Alloc>"
> could not be derived from' const char [16].
> -------
> Many thanks


Hi

It's not true.
I compiled and ran the following code under GCC 4.5.0 and VS 2008:

#include <string>
#include <iostream>
int main()
{
using namespace std;
string str;
string vlstr = str + "sounds/rain.wav";
cout << vlstr << '\n';
return 0;
}

Regards,
-- Saeed Amrollahi
 
Reply With Quote
 
 
 
 
Bo Persson
Guest
Posts: n/a
 
      07-27-2010
Saeed Amrollahi wrote:
> On Jul 27, 9:20 pm, Mike <(E-Mail Removed)> wrote:
>> Hi
>> I've compiled this on Linux:
>>
>> string vlstr = str+"sounds/rain.wav"; //str is a string as well
>>
>> but with VC2008 I get:
>> -----
>> error C2784: 'std:: _String_iterator <_Elem,_Traits,_Alloc> std::
>> operator + (_String_iterator <_Elem,_Traits,_Alloc>::
>> difference_type , std:: _String_iterator <_Elem,_Traits,_Alloc>) ":
>> template argument for 'std:: _String_iterator
>> <_Elem,_Traits,_Alloc>" could not be derived from' const char [16].
>> -------
>> Many thanks

>
> Hi
>
> It's not true.
> I compiled and ran the following code under GCC 4.5.0 and VS 2008:
>
> #include <string>
> #include <iostream>
> int main()
> {
> using namespace std;
> string str;
> string vlstr = str + "sounds/rain.wav";
> cout << vlstr << '\n';
> return 0;
> }
>


Yes, but you also included the <string> header, which I guess Mike
didn't.


Bo Persson


 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      07-28-2010
Thanks. on Lin I had string.h but apparently on Win we need string
without .h
 
Reply With Quote
 
Saeed Amrollahi
Guest
Posts: n/a
 
      07-28-2010
On Jul 28, 8:48*am, Mike <(E-Mail Removed)> wrote:
> Thanks. on Lin I had string.h but apparently on Win we need string
> without .h


Hi

If you want to code using Standard C++, use <string>
on both Linux/GCC and Windows/VS.

Regards,
-- Saeed Amrollahi
 
Reply With Quote
 
Michael Doubez
Guest
Posts: n/a
 
      07-28-2010
On 28 juil, 11:26, Saeed Amrollahi <(E-Mail Removed)> wrote:
> On Jul 28, 8:48*am, Mike <(E-Mail Removed)> wrote:
>
> > Thanks. on Lin I had string.h but apparently on Win we need string
> > without .h

>
> If you want to code using Standard C++, use <string>
> on both Linux/GCC and Windows/VS.


Please note that <string.h> and <string> are not related.
The equivalent of <string.h> in C++ is <cstring>.

--
Michael
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      07-28-2010
Michael Doubez <(E-Mail Removed)> wrote:
> The equivalent of <string.h> in C++ is <cstring>.


Actually both are standard C++ (unless I'm completely misremembering).
Their difference is that the former is guaranteed to pull all the names
defined by the header in the global namespace, while the latter is not
(guaranteed, that is; most practical implementations do it nevertheless).
Basically the idea with the former is to act as a standardized "hack" to
try to maintain some compatibility with pre-standard C++ code.
 
Reply With Quote
 
Michael Doubez
Guest
Posts: n/a
 
      07-29-2010
On 28 juil, 19:17, Juha Nieminen <(E-Mail Removed)> wrote:
> Michael Doubez <(E-Mail Removed)> wrote:
> > The equivalent of <string.h> in C++ is <cstring>.

>
> * Actually both are standard C++ (unless I'm completely misremembering)..
> Their difference is that the former is guaranteed to pull all the names
> defined by the header in the global namespace, while the latter is not
> (guaranteed, that is; most practical implementations do it nevertheless).


No. In this case, those are C headers.

Concerning string.h, this is adressed in 20.4.6/5+6. <cstring> and
<string.h> have the smame content except for modification with memchr
for wide char types.

> Basically the idea with the former is to act as a standardized "hack" to
> try to maintain some compatibility with pre-standard C++ code.


What you refer to are the pre-standard STL header which .h form was
deprecated. I don't remember what used to be the <string> header name
(maybe stl_string.h).

However, the point was here was that <string> and <string.h> are not
related at all.

--
Michael
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      07-29-2010
Michael Doubez <(E-Mail Removed)> wrote:
> On 28 juil, 19:17, Juha Nieminen <(E-Mail Removed)> wrote:
>> Michael Doubez <(E-Mail Removed)> wrote:
>> > The equivalent of <string.h> in C++ is <cstring>.

>>
>> * Actually both are standard C++ (unless I'm completely misremembering).
>> Their difference is that the former is guaranteed to pull all the names
>> defined by the header in the global namespace, while the latter is not
>> (guaranteed, that is; most practical implementations do it nevertheless).

>
> No. In this case, those are C headers.


If they are defined in the C++ standard, then they are C++ headers.

Just because they happen to have the same symbols and functionality as
the equivalent standard C headers doesn't make them any "less C++". A
piece of C++ code which happens to also be valid C doesn't make it any
"less" C++.
 
Reply With Quote
 
Michael Doubez
Guest
Posts: n/a
 
      07-29-2010
On 29 juil, 14:47, Juha Nieminen <(E-Mail Removed)> wrote:
> Michael Doubez <(E-Mail Removed)> wrote:
> > On 28 juil, 19:17, Juha Nieminen <(E-Mail Removed)> wrote:
> >> Michael Doubez <(E-Mail Removed)> wrote:
> >> > The equivalent of <string.h> in C++ is <cstring>.

>
> >> * Actually both are standard C++ (unless I'm completely misremembering).
> >> Their difference is that the former is guaranteed to pull all the names
> >> defined by the header in the global namespace, while the latter is not
> >> (guaranteed, that is; most practical implementations do it nevertheless).

>
> > No. In this case, those are C headers.

>
> * If they are defined in the C++ standard, then they are C++ headers.
>
> * Just because they happen to have the same symbols and functionality as
> the equivalent standard C headers doesn't make them any "less C++". A
> piece of C++ code which happens to also be valid C doesn't make it any
> "less" C++.


Yes. I meant they are the headers inherited from C and not for
backward compatibility reason (contrary to pre-standard stl headers
which are deprecated).

However, this was not the point here; it was Saeed Amrollahi's
confusion about <string> being the <.h suffix>-less version of
<string.h>.

--
Michael
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
the difference between "const char* s" and "char* const s" Gary C Programming 9 08-24-2006 09:57 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Is char** (or char*[]) implicitly convertible to 'const char * const *'? kevin.hall@motioneng.com C Programming 24 10-30-2005 08:07 AM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments