【发布时间】:2011-04-17 01:50:21
【问题描述】:
我正在尝试用 C++ 编写一个简单的时间显示程序。
已编辑
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;
class vClock
{
public:
// constructor
vClock(int = 0, int = 0);
// mutable member functions
void set_time(int, int);
void time_ahead(int);
// constant function
string time_notation(int) const;
void show_time() const;
private:
int hour;
int minute;
int offset_hour;
int offset_minute;
int maxhour;
int maxminute;
int carrier;
};
// member function implementation
vClock::vClock(int hr, int min)
{
hour = hr;
minute = min;
maxhour = 24;
maxminute = 60;
carrier = 0;
}
void vClock::set_time(int hr, int min)
{
hour = hr;
minute = min;
}
void vClock::time_ahead(int add_minute)
{
// suppose to be a short cut for all cases
carrier = ((add_minute + minute) / maxminute);
offset_hour = hour + carrier;
offset_minute = (add_minute + minute) - (carrier * maxminute);
cout << "After " << add_minute << "minutes, the time will be "
<< setfill('0') << setw(2) << offset_hour << ":" << setw(2) << offset_minute << time_notation(offset_hour)<< endl;
return;
}
string vClock::time_notation(int hr) const
{
if(hour < 12)
cout << "AM";
if (hour >= 12)
cout << "PM";
}
void vClock::show_time() const{
cout << setfill('0')
<< setw(2) << hour << ':'
<< setw(2) << minute
<< time_notation(hour) << endl;
return;
}
// member functions implementation END
int main()
{
vClock sample;
sample.set_time(0,59);
// sample.show_time();
sample.time_ahead(118);
return EXIT_SUCCESS;
}
似乎 time_notation 在 cout 语句之前被评估?
*AM*118分钟后,时间为02:57
解决了
我无法再编译它了 - 在我将 time_notation(offset_hour) 添加到 time_ahead() 和 show_time()(位于两个函数体的最后一行)之后,我的 IDE 将崩溃。
在实现该功能并在其他功能中使用之前,编译是可以的。该程序运行良好。
违法吗?
我收到一条很长的错误消息
clock-time.cpp:65: error: no match for 'operator&)(+std::operator&)(+(+std::operator&)(+std::operator&)(+std:: operator&)(+(+std::operator& )(&std::cout)), ((const char*)"After ")))->std::basic_ostream<_chart _traits>::operatorstd::basic_ostream<_chart _traits>::operatorwith _CharT = char, _Traits = std::char_traits)), ((const char*)":")))), std::setw(2)) )->std::basic_ostream<_chart _traits>::operatorwith _CharT = char, _Traits = std::char_traits vClock::time_notation(((vClock*)this) ->vClock::offset_hour)'
我正在使用 MinGW C++ 编译器,我的 IDE 是 jGRASP。 任何帮助表示赞赏。
谢谢!
【问题讨论】: