Sign in to follow this  

how to convert....

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

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
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
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
This works for me. Maybe gives an idea what is wrong.
#include <string>
#include <iostream>

using std::cout;
using std::string;

int main()
{
const char * charstar = "Charstar";
const char chararr [] = "Char-char";

string name;

name = charstar;
cout << name << std::endl;

name = chararr;
cout << name << std::endl;

return 0;
}


Share this post


Link to post
Share on other sites
Quote:
Original post by Boder
This works for me. Maybe gives an idea what is wrong.
*** Source Snippet Removed ***


yes this also works for me.

i just realized that the function that i pass the struct myImage in is passed in as CONST. so that was the problem. sorry for wasting everyones time. SiCrane you were right all along ;)

i guess this would be a good exercise though if passed it in as (const Image &myImage) how would you convert it.

Share this post


Link to post
Share on other sites

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