Convert string char* to Std:string

Asked

Viewed 1,018 times

1

I have the following code:

void splitstr(std::string &modulo, std::string &nmodulo, int &fk)
{
    string frase = modulo;
    string aux = "";
    stringstream strs;

    for (int i = 0; i < frase.length(); i++)
    {
       switch(frase[i])
       {
            case 'c':
                aux = "";
                break;
            default:
                aux = aux + frase[i];
                break;
       }
    }

    nmodulo = aux;
    strs(nmodulo);

    strs >> fk;
}

Whose mistake is:

error: no match for call to '(Std::stringstream {aka Std::basic_stringstream}) (Std::string&)'

1 answer

2

Conversion of Std::string to const char * just use the c_str() function of str::string, eg:

std::string str= "String";
const char * ch_prt= str.c_str();

Conversion of Std::string to char * just use the c_str() function of str::string and copy to a char *, ex:

std::string str= "String";
char * ch_prt = (char*) calloc(str.length()+1, sizeof(char*));
strcpy(ch_prt, str.c_str());

Converting char* or const char* to Std::str is only using direct cast, eg:

char * ch_prt = "LOL";
std::string str = (std::string) ch_prt;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.