Can't assign a char to a string

I have a conversion error when I do this, what is happening?
1
2
  string b = "abc";
  string a = b.back();



Also I am using code block ide to do this, i was wondering if anyone knows how to start the intellisense so that when i type the 'back' I can see some brief info on this back function as a tool tip, like the data type its returning or the available parameters. This intellisense happens when I type things like 'int' or 'string', the tool tips will always appear, but not for this 'back'.
string isn't a char

1
2
3
  std::string b = "abc";
  char a = b.back();
  std::cout << a << '\n';


or
1
2
3
  std::string b = "abc";
  std::string a = std::string(1, b.back());
  std::cout << a << '\n';
You can assign a char to a string, but string a = b.back() is trying to call a constructor, not an assignment operator. There is no constructor that takes a char, but there is this:
string a(1, b.back()); // constructor string::string(size_t n, char c)

Here it is using the assignment operator:
1
2
    string a, b = "abc";
    a = b.back();  // assignment operator string::operator=(char) 

http://www.cplusplus.com/reference/string/string/operator=/
http://www.cplusplus.com/reference/string/string/string/
Why didn't they design the string ctor to take the char first and the repeat factor second and have the repeat default to 1? That seems to make more sense.

 
string::string(char ch, int repeat = 1);

Dutch, I thought the same thing. It seems so obvious that there must be some subtle reason for not doing it.
Topic archived. No new replies allowed.