Jump to content
  • Advertisement
Sign in to follow this  
OpenGL_Guru

how to convert....

This topic is 3776 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

a const char * to an STL String? i have a variable name of type std::string and a const char * NAME; how would assign the const char * to the std::string? i am sure there is a conversion somewhere in there which i am unaware of. thanks!

Share this post


Link to post
Share on other sites
Advertisement

const char* cString = "Booga!";
std::string cppString = cString;
std::string anotherCppString ( cString );


Also note that std::string's constructor isn't explicit, so it supports implicit conversion.


void PrintString( const std::string& stringToPrint )
{
std::cout << stringToPrint;
}

int main ()
{
PrintString ("Booga!");
}

Share this post


Link to post
Share on other sites
Quote:
Original post by MJP

const char* cString = "Booga!";
std::string cppString = cString;
std::string anotherCppString ( cString );


Also note that std::string's constructor isn't explicit, so it supports implicit conversion.


void PrintString( const std::string& stringToPrint )
{
std::cout << stringToPrint;
}

int main ()
{
PrintString ("Booga!");
}



std::string tempString = _NAME;
myImage.name(_NAME);


where the member name of struct myImage is a std::string.

compiler error:
Quote:

error: no match for call to ‘(const std::string) (const char*&)’


i tried this earlier myself and a few other things.. so thats why i ask. and no just assigning it doesnt work either.

Share this post


Link to post
Share on other sites
_NAME is not a good variable name.
And it's weird that it says char*& instead of char*

Share this post


Link to post
Share on other sites
Quote:
Original post by Boder
_NAME is not a good variable name.
And it's weird that it says char*& instead of char*


well _NAME is something that i changed it from. it is named something else but i was making it a general variable on my post.

and yes you are right it does look weird.

in my .h file i say:

const char *name1;
const char *name2;

in the constructor implementation in the .cpp file i assign them...

name1 = "frank";
name2 = "sue";

so i dont know why it doesnt say char* myself


Share this post


Link to post
Share on other sites
std::string tempString = _NAME;
myImage.name = _NAME;

Should work, no reason why it shouldnt, unless "name" is const then cant be changed. Post some code.

Edit:
Wait, post some code, Im lost now.

Share this post


Link to post
Share on other sites
In the .h, try:

const char name1[] = "frank";
const char name2[] = "sue";

myImage.name shouldn't be constant. Does the first example work?

Share this post


Link to post
Share on other sites
Quote:
Original post by Boder
In the .h, try:

const char name1[] = "frank";
const char name2[] = "sue";

myImage.name shouldn't be constant. Does the first example work?


its not const.

struct myImage
{
std::string name;
std::string desc;
};

the basic assignment:

myImage.name = name1 yields:

Quote:

error: passing ‘const std::string’ as ‘this’ argument of ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]’ discards qualifiers



@ Enigma - yes #include<string> is in the class.h file.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!