看板 EE_DSnP 關於我們 聯絡資訊
※ 引述《INsoarDEEP (green)》之銘言: : 標題: [問題] 關於上課的例子 : 時間: Mon Oct 8 20:27:28 2007 : : 我嘗試著打一個像Fig3.3的程式,但是在getline ( cin, myname )的部分compile失敗, : 它說我的"myname" is expected primary-expression,這是什麼意思呢? : (不知道這問題會不會問的不好...) : : #include <iostream> : #include <string> : using namespace std; : : class MyClass : {public : : void function1(string myname) : { : cout << " Nice to meet you ," << myname << '.' << endl; : } : : }; : int main() : { : cout << "Input your name."; : string myname; : MyClass sayhello; : : getline( cin, string myname ); : sayhello.function1(string myname); : system ("pause"); : return 0; : } : : : : -- : ※ 發信站: 批踢踢實業坊(ptt.cc) : ◆ From: 140.112.236.234 : 推 AlanLee76083:要先宣告 string myname 10/08 23:03 : 推 INsoarDEEP:呃...宣告字串要用什麼符號呢? 10/08 23:17 : 推 AlanLee76083:#include<string> 10/08 23:23 : 推 INsoarDEEP:還是COMPILE錯誤阿...OTZ 10/08 23:32 : 推 ilway25:你拿CD中的檔案compile & 比較一下看看 10/08 23:39 : 推 ric2k1:It's will be easier if you can post the complete code 10/09 00:38 : → ric2k1:and the error message... 10/09 00:39 : ※ 編輯: INsoarDEEP 來自: 140.112.236.234 (10/09 17:07) : 推 INsoarDEEP:已經加上程式碼了,煩請各位指點一下,感謝! 10/09 17:07 : 推 timrau:getline(cin, string myname) -> getline(cin, myname) 10/09 23:40 : → timrau:sayhello.function1(string myname)也是把string拿掉 10/09 23:41 Note: In function "definition", you need to specify variable type. For example, void f(int a, string str) { ... } But when you call the function, just pass in variables or constant as arguments. For example, f(10, myname); or f(a, myname); or f(a, "Ric"); -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.121.131.190