看板 Programming 關於我們 聯絡資訊
謝謝 adrianshum, 的確是 public vs Public 問題... 以下是更正後的版本, 還有兩個問題. 1. 不知道為什麼, cout 不能用(說未宣告), 我已經 using std 了說. 所以,下面改用 printf, 引用 stdio.h 2. 現在Compile 後的錯誤看不懂.... 單純使用 gcc -o c c.cpp 進行 compile c.cpp -------------------------------------------------- #include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; class Time { public: Time(); void setTime(int, int, int); void printUniversal(); void printStandard(); private: int hour; int minute; int second; }; Time::Time(){ hour = minute = second = 0; } void Time::setTime(int h, int m, int s) { hour = ((h>=0)&&(h<=23)?h:0); minute = ((m>=0)&&(m<=59)?m:0); second = ((s>=0)&&(s<=59)?s:0); } void Time::printUniversal() { printf("%02d:%02d:%02d\r\n",hour,minute,second); }; void Time::printStandard() { printf("%02d:%02d:%02d %s\r\n",((hour == 0 || hour == 12) ? 12 : hour % 12) ,minute,second,(hour < 12 ? "AM": "PM")); }; int main () { Time t; t.printUniversal(); t.printStandard(); t.setTime(23,59,59); t.printUniversal(); t.printStandard(); } ---------------------------------------------------------- Compile的錯誤為 ---------------------------------------------------------- /tmp/ccGL6ZGt.o: In function `__static_initialization_and_destruction_0(int, int)': c.cpp:(.text+0x202): undefined reference to `std::ios_base::Init::Init()' c.cpp:(.text+0x207): undefined reference to `std::ios_base::Init::~Init()' /tmp/ccGL6ZGt.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0' collect2: ld returned 1 exit status ---------------------------------------------------------- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.161.187.93 ※ 編輯: longrider 來自: 118.161.187.93 (01/07 00:34)
sunneo:用g++吧 118.171.82.147 01/07 00:42
xam:有用到stl, 就用g++編譯 114.32.92.137 01/07 01:49
dryman:gcc換成g++,就可以用class和cout了 114.42.91.41 01/07 11:36
longrider:感謝各位, 換 g++ 果然就沒錯誤訊息了 140.115.34.47 01/07 15:04