地下室骑木马调教:急求C++编译时候的错误解决方法

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/28 07:41:43
我编写了一个简单的C++程序,但是出现了这个错误,请高手指点,谢谢!
这里定义了一个头文件0502.h
#include<iostream.h>
class Time{
public:
Time(int hour=10,int minute=20,int seconds=30);

Time();
void showtime();
private:
int hrs,mins,secs;
};
Time::Time(int hour,int minute,int seconds)
{
hrs=hour;
mins=minute;
secs=seconds;
}
Time::Time()
{
hrs=15;
mins=25;
secs=35;
}
void Time::showtime()
{
cout<<hrs<<":"<<mins<<":"<<secs<<endl;
}
这是主函数
#include<iostream.h>
#include"time0502.h"
int main()
{
cout<<"show the every time object"<<endl;
Time aTime;
cout<<"*****aTime***:";
aTime.showtime();

Time bTime(12);
cout<<"*****bTime***:";
bTime.showtime();

Time cTime(12,30,50);
cout<<"*****cTime***:";
cTime.showtime();
cout<<"End of my class Time!!!!"<<endl;
return 0;
}

编译的时候出了这个问题
请高手帮忙解决,谢谢!
d:\新建文件夹\msdev98\myprojects\time0502\0502h.cpp(0) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
Error executing cl.exe.
我试了一下楼下的答案,但是好象还是不行,不晓得还有没有其他高手的答案,不过还是谢谢了!!


Time();
这个构造函数删掉~~~
显然在main中执行
Time aTime;
时会有歧义 不知道该用哪个构造函数

我觉得可能是在定义那个“构造函数time”出了问题,不应该把构造函数里面的形参表里初始化, 只能在建立对象时初始化,可以为:
#include<iostream>
using namespace std;
class Time
{ public:
Time(int h=15,int m=25,int s=35):hour(h),minutes(m),seconds(s){}
time(){};
void showtime();
private:
int hour,minutes,seconds;
};
Time::Time(int h,int m,int s)
{
hour=h;
minutes=m;
seconds=s;
}
void Time::showtime()
{
cout<<hour<<":"<<minutes<<":"<<seconds<<endl;
}

#include"time0502.h"
#include<iostream>//还是要按标准C++格式写哦!
using namespace std;
int main()
{
cout<<"show the every time object"<<endl;
Time aTime;
cout<<"*****aTime***:";
aTime.showtime(); //调用第二的构造函数time()里面没有任何语句,
//此时无数据输出

aTime(12);//仍使用这个对象,把12付给 hour ,
//此时调用第一个构造函数
cout<<"*****aTime***:";
aTime.showtime();

aTime(12,30,50);
cout<<"*****aTime***:";
aTime.showtime();
cout<<"End of my class Time!!!!"<<endl;
return 0;
}

是构造函数的问题,如果在一个类中定义了全部是默认参数的构造函数后,不能再定义重载构造函数.

此题中的Time(int h=15,int m=25,int s=35);是全部由默认参数组成的构造函数.
所以不能再定义Time; 或其它的构造函数.

头文件改为
-----------------------------------------------------------
#include<iostream.h>
class Time{
public:
Time(int a,int hour=10,int minute=20,int seconds=30);
Time();
void showtime();
private:
int hrs,mins,secs;
};

Time::Time(int a,int hour,int minute,int seconds)
{
hrs=hour;
mins=minute;
secs=seconds;
}
Time::Time()
{hrs=15;
mins=25;
secs=35;
}

void Time::showtime()
{
cout<<hrs<<":"<<mins<<":"<<secs<<endl;
}
-----------------------------------------------------------

主函数改为
-----------------------------------------------------------
#include<iostream.h>
#include"Time.h"
int main()
{
cout<<"show the every time object"<<endl;
Time aTime;
cout<<"*****aTime***:";
aTime.showtime();

Time bTime(0,12);
cout<<"*****bTime***:";
bTime.showtime();

Time cTime(0,12,30,50);
cout<<"*****cTime***:";
cTime.showtime();
cout<<"End of my class Time!!!!"<<endl;
return 0;
}
-----------------------------------------------------------
其中参数a只是为了让计算机识别该调用哪个构造函数,

-----------------------------------------------------------

--------------------------------------------------------------