作者longrider (龙骑士2)
看板Programming
标题Re: [问题] 请教 C++ 的基本问题
时间Thu Jan 7 00:34:34 2010
谢谢 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)
1F:推 sunneo:用g++吧 118.171.82.147 01/07 00:42
2F:→ xam:有用到stl, 就用g++编译 114.32.92.137 01/07 01:49
3F:推 dryman:gcc换成g++,就可以用class和cout了 114.42.91.41 01/07 11:36
4F:→ longrider:感谢各位, 换 g++ 果然就没错误讯息了 140.115.34.47 01/07 15:04