2012-10-21 23 views
0

當我運行此程序我得到在管線s<<"\""<<string<<"\""下面提到錯誤:誤差在該C++程序

#include <string> 
#include <iostream> 
#include <sstream> 
#include <cstdlib> 

using namespace std; 
string str="abc"; 
stringstream s; 
s<<"\""<<string<<"\""; 
cout<<(s.str().c_str()); 

錯誤:預期構造,析構函數,或類型轉換之前「< <」令牌 彙編終止,因爲重大錯誤。

http://codepad.org/KuyMQg3x,這裏是在線錯誤代碼。

+3

你是不是指'str'而不是'string'? – chris

+0

你把字符串作爲變量名(太晚:) – nico

+0

同樣的錯誤,更正後。 – rajat

回答

0

您試圖在頂層而不是在函數內部執行代碼。這就是你的程序應該看起來像:

#include <string> 
#include <iostream> 
#include <sstream> 
#include <cstdlib> 

using namespace std; 

int main() { 
    string str="abc"; 
    stringstream s; 
    s<<"\""<<str<<"\""; 
    cout<<(s.str().c_str()); 
} 
0

你似乎缺少你的主要功能。試試這個:

#include <string> 
#include <iostream> 
#include <sstream> 
#include <cstdlib> 

using namespace std; 

int main() { 
    string str="abc"; 
    stringstream s; 
    s<<"\""<<str<<"\""; 
    cout<<(s.str().c_str()); 
} 

C++運行時需要的功能來運行啓動程序時,它會始終運行int main()(或int main(int argc, char* argv)

2
#include <string> 
#include <iostream> 
#include <sstream> 


int main() { 
    using namespace std; 
    string str="abc"; 
    stringstream s; 
    s<<"\""<<str<<"\""; 
    std::cout<<(s.str().c_str()); 

} 

的評論改變字符串中建議STR工作正常。