我有,
时间.h:
#pragma once
#ifndef TIME_H
#define TIME_H
class time
{
private:
int hour;
int minute;
int second;
public:
enum pmORam {am,pm};
time(void);
time(int,int,int);
void printtime(void);
time subtime(time &);
const time &operator=(const time &);
void setTime(int,int,int);
~time(void);
};
#endif
时间.cpp:
#include <iostream>
#include <iomanip>
#include "stdafx.h"
#include "time.h"
using namespace std;
time::time(void)
{
hour=0;
minute=0;
second=0;
}
time::time(int hr,int min,int sec)
{
setTime(hr,min,sec);
}
void time::printtime(void)
{
enum pmORam p;
if(hour<=12 && hour>=0)
cout<<setfill('0')<<setw(2)<<hour<<":"<<setw(2)<<minute<<":"<<setw(2)<<second<<p.am<<endl;
else
if(hour>12 && hour<=23)
{
hour=hour%12;
cout<<setfill('0')<<setw(2)<<hour<<":"<<setw(2)<<minute<<":"<<setw(2)<<second<<p.pm<<endl;
}
}
const time &time::operator=(const time &t)
{
hour=t.hour;
minute=t.minute;
second=t.second;
return *this;
}
time time::subtime(time &m)
{
if(hour>m.hour)
{
if(m.second>second)
{
minute=minute-1;
second=(second+60)-m.second;
}
second=second-m.second;
if(m.minute>minute)
{
hour=hour-1;
minute=(minute+60)-m.minute;
}
minute=minute-m.minute;
hour=hour-m.hour;
}
if(second>m.second)
{
m. minute=m.minute-1;
m.second=(m.second+60)-second;
}
m.second=m.second-second;
if(minute>m.minute)
{
m.hour=m.hour-1;
m.minute=(m.minute+60)-minute;
}
m.hour=hour-m.hour;
return *this;
}
void time::setTime(int hr,int min,int sec)
{
hour=(hr>=0 && hr<=23)? hr: 0;
minute=(min>=0 && min<60)? min:0;
second=(sec>=0 && sec<60)? sec:0;
}
time::~time(void)
{
}
但是在 time.cpp 中,我遇到了这个错误:
IntelliSense: declaration is incompatible with "time time::subtime(time &)"
IntelliSense: declaration is incompatible with "const time &time::operator=(const time &)"
'time::time(const time &)' : cannot convert parameter 1 from 'time *const ' to 'const time &'
'int time::subtime(time &)' : overloaded function differs only by return type from 'time time::subtime(time &)'
'int &time::operator =(const time &)' : overloaded function differs only by return type from 'const time &time::operator =(const time &)'
function "time" is not a type name
什么是问题?
编辑:修复命名冲突的新代码
时间.cpp:
#include <iostream>
#include <iomanip>
#include "stdafx.h"
#include "time.h"
using namespace std;
timea::timea(void)
{
hour=0;
minute=0;
second=0;
}
timea::timea(int hr,int min,int sec)
{
setTime(hr,min,sec);
}
void timea::printtime(void)
{
enum timea::pmORam p;
if(hour<=12 && hour>=0)
cout<<setfill('0')<<setw(2)<<hour<<":"<<setw(2)<<minute<<":"<<setw(2)<<second<<p.am<<endl;
else
if(hour>12 && hour<=23)
{
hour=hour%12;
cout<<setfill('0')<<setw(2)<<hour<<":"<<setw(2)<<minute<<":"<<setw(2)<<second<<p.pm<<endl;
}
}
const timea &timea::operator=(const timea &t)
{
hour=t.hour;
minute=t.minute;
second=t.second;
return *this;
}
timea timea::subtime(timea &m)
{
if(hour>m.hour)
{
if(m.second>second)
{
minute=minute-1;
second=(second+60)-m.second;
}
second=second-m.second;
if(m.minute>minute)
{
hour=hour-1;
minute=(minute+60)-m.minute;
}
minute=minute-m.minute;
hour=hour-m.hour;
}
if(second>m.second)
{
m. minute=m.minute-1;
m.second=(m.second+60)-second;
}
m.second=m.second-second;
if(minute>m.minute)
{
m.hour=m.hour-1;
m.minute=(m.minute+60)-minute;
}
m.hour=hour-m.hour;
return *this;
}
void timea::setTime(int hr,int min,int sec)
{
hour=(hr>=0 && hr<=23)? hr: 0;
minute=(min>=0 && min<60)? min:0;
second=(sec>=0 && sec<60)? sec:0;
}
timea::~timea(void)
{
}