头文件 mytime.h
#ifndef MYTIME1_H #define MYTIME1_H class MyTime { private: int hour; int min; int sec; public: MyTime(int hour = 0, int min = 0, int sec = 0); void run(); void setTime(int h = 12, int m = 12, int s = 12); private: void show(); void dida(); }; #endif
主程序 test.cpp
#include "mytime.h" void main() { MyTime mt; mt.setTime(); mt.run(); }
mytime.cpp
#include <iostream> #include <iomanip> #include <windows.h> #include "mytime.h" using namespace std; MyTime::MyTime(int hour, int min, int sec):hour(hour),min(min),sec(sec) { } void MyTime::setTime(int h, int m, int s) { hour = h; min = m; sec = s; } void MyTime::run() { while (1) { show(); Sleep(1000); dida(); } } void MyTime::show() { cout << setfill('0') << setw(2) << hour << ':' << setw(2) << min << ':' << setw(2) << sec << '\r' << flush; } void MyTime::dida() { if (60 == ++sec) { sec = 0; if (60 == ++min) { min = 0; if (24 == ++hour) { hour = 0; } } } }
运行后出现如下错误:
C2059 语法错误:")"
C4003 "min宏的实参不足
问题解决
头文件<windows.h>中定义了min,max的宏,导致导入相应头文件后初始化列表处min() 冲突。
修改成员变量min的名字即可