大家看看这个简单的程序是怎么了呀?(高手莫入)

yuanxulong198010 2002-11-06 08:13:13
下面是我写的一个简单的小程序,但是无法编译通过!
#include <iostream.h>
class mytime
{
public:
int tm_hour;
int tm_min;
int tm_sec;
public:
void get()
{
cout<<tm_hour<<":"<<tm_min<<":"<<tm_sec<<endl;
}
void set(int,int,int);
}
保存为mytime.h文件。
#include "mytime.h"
mytime::set(int h,int m,int s)
{
hour=h;
min=m;
sec=s;
}
保存为mytime.cpp文件。
然后是主文件,如下:
void main() //error:too many type proto>>
{
mytime mtime;
mtime.set(12,35,35);
mtime.get();
} //warning:function should return a value;

为什么不能编译通过呀,是不是我定义的头文件有问题呀,请指教!

...全文
66 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hzh_shat 2002-11-06
  • 打赏
  • 举报
回复
//--------mytime.h-------
#include <iostream.h>
class mytime
{
public:
int tm_hour;
int tm_min;
int tm_sec;
public:
void get() {cout<<tm_hour<<":"<<tm_min<<":"<<tm_sec<<endl;}
void set(int h,int m,int s)
{
tm_hour=h;
tm_min=m;
tm_sec=s;
}
};
//-------main.cpp----------
#include"mytime.h"
int main()
{
mytime mtime;
mtime.set(12,35,35);
mtime.get();
return 0;
}


h3wolf 2002-11-06
  • 打赏
  • 举报
回复
//--------mytime.h-------
#include <iostream.h>
class mytime
{
public:
int tm_hour;
int tm_min;
int tm_sec;
public:
void get() {cout<<tm_hour<<":"<<tm_min<<":"<<tm_sec<<endl;}
void set(int,int,int);
}

//--------mytime.cpp-------
#include "mytime.h"
mytime::set(int h,int m,int s)
{
tm_hour=h;
tm_min=m;
tm_sec=s;
}

//-------main.cpp----------
#include "mytime.h"

int main()
{
mytime mtime;
mtime.set(12,35,35);
mtime.get();

return 0;
}
h3wolf 2002-11-06
  • 打赏
  • 举报
回复
把void main()改为int main()
h3wolf 2002-11-06
  • 打赏
  • 举报
回复
hour,min,sec都没定义,主文件应包含"mytime.h",不能通过编译
earphone 2002-11-06
  • 打赏
  • 举报
回复
mytime::set(int h,int m,int s)
{
hour=h;
min=m;
sec=s;
}

你的类中没有定义hour,min,sec三个变量
langzi8818 2002-11-06
  • 打赏
  • 举报
回复
#include <iostream.h>
class mytime
{
public:
int tm_hour;
int tm_min;
int tm_sec;
public:
void get()
{
cout<<tm_hour<<":"<<tm_min<<":"<<tm_sec<<endl;
}
void set(int,int,int);
}//这里缺";"
保存为mytime.h文件。
#include "mytime.h"
mytime::set(int h,int m,int s)//应该加void
{
hour=h;//变量不对
min=m;
sec=s;
}
保存为mytime.cpp文件。
然后是主文件,如下://加#include"mytime.h"
void main() //error:too many type proto>>
{
mytime mtime;
mtime.set(12,35,35);
mtime.get();
} //warning:function should
没有问题了。你好好看看基础的东西吧。
langzi8818 2002-11-06
  • 打赏
  • 举报
回复
#include <iostream.h>
class mytime
{
public:
int tm_hour;
int tm_min;
int tm_sec;
public:
void get()
{
cout<<tm_hour<<":"<<tm_min<<":"<<tm_sec<<endl;
}
void set(int h,int m,int s);
};

void mytime::set(int h,int m,int s)
{
tm_hour=h;
tm_min=m;
tm_sec=s;
}

void main() //error:too many type proto>>
{
mytime mtime;
mtime.set(12,35,35);
mtime.get();
} //warning:function sh
没有问题了
ji_da_sheng 2002-11-06
  • 打赏
  • 举报
回复
//--------mytime.h-------
#include <iostream.h>
class mytime
{
public:
int tm_hour;
int tm_min;
int tm_sec;
public:
void get() {cout<<tm_hour<<":"<<tm_min<<":"<<tm_sec<<endl;}
void set(int,int,int);
}

//--------mytime.cpp-------
#include "mytime.h"
void mytime::set(int h,int m,int s)
{
tm_hour=h;
tm_min=m;
tm_sec=s;
}

//-------main.cpp----------
#include "mytime.h"

void main()
{
mytime mtime;
mtime.set(12,35,35);
mtime.get();
}

dlqcsdn 2002-11-06
  • 打赏
  • 举报
回复
还有一点
就是
在mytime.cpp里面的
mytime::set(int h,int m,int s)
前面还要加一个void
yuanxulong198010 2002-11-06
  • 打赏
  • 举报
回复
其实我在void main()前已经加有头文件#include <mytime.h>,但是不能通过,说变量没有定义,是不是应该这样?
//mytime.cpp
#include <mytime.h>
mytime::set(int h,int m,int s)
{
mytime::hour=h;
mytime::min=m;
mytime::sec=s;
}
是不是这样定义呢,还有呀,是不是只要包含***.h,系统就自动查找它的.cpp文件呀!

70,032

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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