[問題] C++的constructor

看板C_and_CPP作者 (little white)時間15年前 (2011/02/07 15:32), 編輯推噓6(606)
留言12則, 8人參與, 最新討論串1/1
各位前輩好 最近再家自學C++ 遇到一點小問題 #include<iostream> #include<string> using std::cout; using std::endl; using std::string; class Gradebook { public: GradeBook( string name) { setCourseName(name); } void setCourseName(string name) { courseName = name; } string getCourseName() { return courseName; } void displaymessage() { cout << "Welcome to the grade book for\n" << getCourseName() << "!!" <<endl; } private: string courseName; }; int main() { GradeBook gradeBook1("CS101 Introduction to C++"); GradeBook gradeBook2("CS102 Data Structure"); cout << "gradebook1 created for course: " << gradebook1.getCourseName() << "\ngradebook2 created for course: " << gradebook2.getCourseName(); return 0; } 當我Compile的時候DEV C++說 ISO C++ forbids declaration of `GradeBook' with no type 可GradeBook是constructor不是不用type嗎? 煩請各位大大幫小的我解答 第一次發文請多包涵 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.62.56.236

02/07 15:42, , 1F
Grade"b"ook Grade"B"ook
02/07 15:42, 1F

02/07 15:45, , 2F
大小寫不一樣會當成不同的東西
02/07 15:45, 2F

02/07 16:02, , 3F
推在家自學
02/07 16:02, 3F

02/07 17:53, , 4F
恩恩 感謝各位大大的指導
02/07 17:53, 4F

02/07 18:32, , 5F
這個例子告訴我們複製貼上是很重要的 XD
02/07 18:32, 5F

02/07 19:32, , 6F
推同在家自學
02/07 19:32, 6F

02/07 23:18, , 7F
推薦code::block他會自動告知變數名稱
02/07 23:18, 7F

02/08 11:03, , 8F
可我用CODEBLOCK不能COMPILE耶QQ
02/08 11:03, 8F

02/08 13:58, , 9F
你是不是裝沒有GNU gcc的版本...
02/08 13:58, 9F

02/08 23:27, , 10F
我官網兩個版本都裝過了 要compile都沒反應
02/08 23:27, 10F

02/08 23:27, , 11F
我compile是選gnu gcc那個
02/08 23:27, 11F

02/09 00:02, , 12F
莫名把code blocks弄好了 感謝各位前輩!!
02/09 00:02, 12F
文章代碼(AID): #1DJw0Ifo (C_and_CPP)