我要提问

狂砍八条街 2008-11-26 07:48:20
这是源程序:
// Fig. 6.10: fig06_lO.cpp
//#include <iostream.h>
// Demonstrating the Time class set and get functions
#include<iostream.h>
#include "time3.h"

void incrementMinutes(Time &, const int );

int main()
{
Time t;

t.setHour(17);
t.setMinute(34);
t.setSecond(25);

cout<<"Result of setting all valid values:\n"
<<"Hour: "<< t.getHour()
<< " Minute: " << t.getMinute()
<<" Second: "<< t.getSecond();

t.setHour( 234 ); // invalid hour set to 0
t.setMinute( 43 );
t.setSecond( 6373 ); // invalid second set to 0

cout <<"\n\nResult of attempting to set invalid hour and"
<<"second:"<<"Hour: "<< t.getHour()
<<" Minute: "<< t.getMinute()
<<" Second: "<< t.getSecond() << "\n\n";

t.setTime( 11, 58, 0 );
incrementMinutes( t, 3 );

return 0;
}

void incrementMinutes(Time &tt, const int count)
{
cout<<"Incrementing minute" << count
<< "times:\nStart time: ";
tt.printStandard();

for (int i = 0; i < count; i++ ) {
tt.setMinute( (tt.getMinute() + 1 ) % 60);

if (tt.getMinute() == 0 )
tt.setHour( ( tt.getHour() + 1 ) % 24);

cout << "\nminute + 1: ";
tt.printStandard();
}

cout << endl;
}
报错:Compiling...
time3.cpp
d:\program files\microsoft visual studio\vc98\include\iostream.h(50) : error C2628: 'Time' followed by 'long' is illegal (did you forget a ';'?)
d:\program files\microsoft visual studio\vc98\include\streamb.h(65) : error C2371: 'streampos' : redefinition; different basic types
d:\program files\microsoft visual studio\vc98\include\iostream.h(50) : see declaration of 'streampos'
d:\program files\microsoft visual studio\vc98\include\streamb.h(65) : error C2371: 'streamoff' : redefinition; different basic types
d:\program files\microsoft visual studio\vc98\include\iostream.h(50) : see declaration of 'streamoff'
d:\program files\microsoft visual studio\vc98\include\istream.h(62) : error C2371: 'streamoff' : redefinition; different basic types
d:\program files\microsoft visual studio\vc98\include\iostream.h(50) : see declaration of 'streamoff'
d:\program files\microsoft visual studio\vc98\include\istream.h(62) : error C2371: 'streampos' : redefinition; different basic types
d:\program files\microsoft visual studio\vc98\include\iostream.h(50) : see declaration of 'streampos'
d:\program files\microsoft visual studio\vc98\include\ostream.h(57) : error C2371: 'streamoff' : redefinition; different basic types
d:\program files\microsoft visual studio\vc98\include\iostream.h(50) : see declaration of 'streamoff'
d:\program files\microsoft visual studio\vc98\include\ostream.h(57) : error C2371: 'streampos' : redefinition; different basic types
d:\program files\microsoft visual studio\vc98\include\iostream.h(50) : see declaration of 'streampos'
使用set和get函数.cpp
j:\wjy\erro\fig_06_10\使用set和get函数.cpp(7) : error C2143: syntax error : missing ';' before 'PCH creation point'
Error executing cl.exe.

fig_06_10.exe - 8 error(s), 0 warning(s)
请问一下高手,怎么解决这个问题,还有报错的原因是什么?
...全文
178 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
狂砍八条街 2008-11-26
  • 打赏
  • 举报
回复
// Fig. 6.10: time3.cpp
// Member function defintions for Time class
#include "time3.h"
#include <iostream.h>

// Constructor function to initialize private data.
// Default values are 0 (see class definition).
Time::Time( int hr, int min, int sec )
{
setTime( hr, min, sec );
}
// Set the values of hour, minute, and second.
void Time::setTime(int h,int m,int s)
{
setHour( h );
setMinute( m );
setSecond( s );
}

// Set the hour value
void Time::setHour(int h)
{
hour=(h>0 && h<24) ? h: 0;
}

// Set the minute value
void Time::setMinute(int m)
{
minute=(m>=0 && m<60) ? m : 0;
}
// Set the second value
void Time::setSecond(int s)
{
second = (s>=0 && s<60) ? s : 0;
}

// Get the hour value
int Time::getHour() { return hour;}

// Get the minute value
int Time::getMinute() { return minute; }

// Get the second value
int Time::getSecond() { return second; }

// Print time is military format
void Time::printMilitary()
{
cout<<(hour<10 ? "0" : "")<<hour<<":"
<<(minute<10 ? "0" : "")<<minute;
}

// Print time in standard format
void Time::printStandard()
{
cout<<((hour==0 || hour == 12) ? 12 : hour%12 )
<<":"<<(minute< 10 ? "0" : "")<<minute
<< ":" <<(second < 10 ? "0" : "" )<<second
<<(hour<12 ? "AM" : "PM");
}
狂砍八条街 2008-11-26
  • 打赏
  • 举报
回复
这是time3头文件
// Fig. 6.10: time3.h
// Declaration of the Time class.

// preprocessor directives that
// prevent multiple inclusions of header file
#ifndef TIME3_H
#define TIME3_H

class Time
{
public:
Time( int = 0, int = 0, int= 0 ); // constructor

// set functions
void setTime( int, int, int ); // set hour, minute, second
void setHour( int ); // set hour
void setMinute( int ); // set minute
void setSecond( int ); // set second

// get functions
int getHour(); // return hour
int getMinute(); // return minute
int getSecond(); // return second

void printMilitary(); // output military time
void printStandard(); // output standard time//

private:
int hour; // 0 - 23
int minute; // 0 - 59
int second; // 0 - 59
}

#endif

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧