Re: [問題] 請教 C++ 的基本問題
謝謝 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)
推
01/07 00:42, , 1F
01/07 00:42, 1F
→
01/07 01:49, , 2F
01/07 01:49, 2F
推
01/07 11:36, , 3F
01/07 11:36, 3F
→
01/07 15:04, , 4F
01/07 15:04, 4F
討論串 (同標題文章)
完整討論串 (本文為第 3 之 3 篇):