Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String c++ manipulation

Tags:

c++

string

c++11

Why the following code have compilation error?

#include<iostream>
#include<string>
using namespace std;

int main()
{
    string str="abc";

    string result=str[0];
    cout<<result<<endl;
    return 0;
}

However, the following code works fine:

#include<iostream>
#include<string>
using namespace std;

int main()
{
    string str="abc";

    str=str[0];
    cout<<str<<endl;
    return 0;
}

I works in unix and compilation command is: "g++ -g test.cpp -std=c++11 -o a", thenm ./a

The error for the first test.cpp after compile is:

test.cpp:9:21: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
  string result=str[0];
                     ^
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/string:52:0,
                 from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/locale_classes.h:40,
                 from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/ios_base.h:41,
                 from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:42,
                 from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
                 from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
                 from test.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/basic_string.h:490:7: error:   initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' [-fpermissive]
       basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
       ^
like image 609
user3259711 Avatar asked Feb 22 '26 12:02

user3259711


2 Answers

std::basic_string doesn't have any constructor that takes a single CharT argument. This means that std::string (i.e. std::basic_string<char>) cannot be constructed from a single char.

The class does, however, have an assignment operator overload that takes a single CharT argument, which is why your second example compiles.

The difference between the two cases is because in the first you're performing copy initialization, which means technically you're first attempting to construct a temporary std::string instance from the char argument, and then copy it over to result. In the second you're performing assignment, which means assigning a new value to an existing std::string instance.


basic_string does have a constructor that takes a count followed by a character:

basic_string(size_type count, CharT ch, const Allocator& alloc = Allocator());

so your original example would compile if you changed the offending line to

string result = {1, str[0]};
like image 67
Praetorian Avatar answered Feb 24 '26 03:02

Praetorian


The following code works fine as well:

string result;
result=str[0];

That means the difference is between initialization and simple assignment and, if you examine the error:

error: invalid conversion from ‘char’ to ‘const char*’

it should be clear that the initialization is not as "full-featured" as assignment - the is no string constructor that takes a char argument (there is an assignment that takes a char which is why your second example works).

You can fix it (in one way, there's no doubt others as well) by ensuring you initialize with a string rather than a character:

string result = str.substr(0,1);
like image 44
paxdiablo Avatar answered Feb 24 '26 05:02

paxdiablo