我是C++的新手,還沒有完全掌握所有的概念,所以我很困惑爲什麼這個函數不起作用。我目前不在家,所以我不能發佈編譯器錯誤,只要我回家,我會盡快完成。返回一個常量字符指針會產生一個錯誤
這是功能。
const char * ConvertToChar(std::string input1, std::string input2) {
// Create a string that you want converted
std::stringstream ss;
// Streams the two strings together
ss << input1 << input2;
// outputs it into a string
std::string msg = ss.str();
//Creating the character the string will go in; be sure it is large enough so you don't overflow the array
cont char * cstr[80];
//Copies the string into the char array. Thus allowing it to be used elsewhere.
strcpy(cstr, msg.c_str());
return * cstr;
}
它使連接和轉換兩個字符串在一起,以返回一個const char *。那是因爲我想用它的函數需要一個const char指針來傳遞。
字符串可以更容易連接:'string1 + string2'。你不需要整個'stringstream'的東西。 –