Sign in to follow this  

[C++] Casting issue with GCC/MinGW implementation of std::string

This topic is 2961 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been getting an error that puzzles me lately while working with Qt and MinGW's std::string implementation. Apparently, I cannot assign a single char to an std::string.
// This code
std::string str = 'x';

// produces this error:
C:/C++/Boron/src/Parser/lexer.cpp:337: error: invalid conversion from `char' to `const char*'
All information I can find suggests that I should be allowed to do this. All my usual references (particularly Cplusplus.com) have example code that states that this will work. I even went into the MinGW std::basic_string header and poked around for the operator=() implementation, and it exists. Yet I get this casting error, which I thought would only have been raised if std::string::operator=(char) was not defined. edit: I ran the following program from cplusplus.com's std::string through GCC:
// string assigning
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str1, str2, str3;
  str1 = "Test string: ";   // c-string
  str2 = 'x';               // single character
  str3 = str1 + str2;       // string

  cout << str3  << endl;
  return 0;
}
Now, this compiles and runs just fine, but if I add the following lines to the end of the program, I get that same casting error that I got before:
string str4 = 'y';
cout << str4 << endl;
I haven't worked in C++ much for a little while, and I've never run across this particular issue. What is going on here? Is there something wrong with my MinGW installation, or is this some bug in GCC's implementation of std::string? [Edited by - ApochPiQ on November 5, 2009 7:22:48 PM]

Share this post


Link to post
Share on other sites
This line:

std::string str = 'x';


does not invoke std::string's operator=(char_t c) but a constructor that takes a single char as a parameter*, which does not exist for std::string. You want:

std::string str(1, 'x');


[*] Even though there's an '=' symbol here, this is initialization, not assignment, and so operator=() is never called in this context. More precisely, this is copy initialization, and in this case is the same as writing std::string str(std::string('x')), although the compiler is allowed to optimize away the copy construction of the temporary. See this.

[Edited by - Metsan on November 5, 2009 5:10:36 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Metsan
This line:

std::string str = 'x';

does not invoke std::string's operator=(), but a constructor that takes a single char as a parameter, which does not exist for std::string. You want:

std::string str(1, 'x');


Works, thanks. I didn't know about that particular constructor.

Share this post


Link to post
Share on other sites

This topic is 2961 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this