我有一個簡單的函數來構建一個簡單的'Movie'對象的字符串表示。當我加入這行來測試值是有效的,我這樣做...爲什麼我的簡單字符串沒有正確構建?
string Movie::getDisplayText() {
ostringstream oss;
oss << "Title: " << this->getTitle() << "\tYear: "+this->getYear() << "\tGenre: " << this->getGenre();
string ret = oss.str();
return ret;
}
但是被建成並返回的字符串看起來像這樣...
\�tle: Star Wars�B
Genre: Science-Fiction
...
cout << "DEBUG Title: " << this->getTitle() << "\tYear: "+this->getYear() << "\tGenre: " << this->getGenre() << "\n" << endl;
...它輸出到「COUT」正確尋找字符串,所以我知道這些值都正確初始化...
DEBUG title='Star Wars'; year='1977'; genre='Science-Fiction';
我的ostringstream代碼有什麼問題?
將整數添加到字符串文字不會進行連接。 – chris
恩,呃。謝謝! –