LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1125|回复: 6

有关用g++编译c++中类的问题

[复制链接]
发表于 2003-10-23 22:38:39 | 显示全部楼层 |阅读模式
今天开始学c++中的类了
但是第一个程序就被g++给出了一大堆错误,错误如下
1.cc:14: semicolon missing after declaration of `clock'
1.cc:15: ISO C++ forbids defining types within return type
1.cc:15: two or more data types in declaration of `settime'
1.cc:15: prototype for `clock clock::settime(int, int, int)' does not match any
   in class `clock'
1.cc:8: candidate is: void clock::settime(int = 0, int = 0, int = 0)
1.cc:15: `clock clock::settime(int, int, int)' and `void clock::settime(int =
   0, int = 0, int = 0)' cannot be overloaded
1.cc:15: semicolon missing after declaration of `class clock'
1.cc: In member function `int clock::settime(clock*, int, int, int)':
1.cc:17: `mimute' undeclared (first use this function)
1.cc:17: (Each undeclared identifier is reported only once for each function it
   appears in.)
1.cc: In function `int main()':
1.cc:28: `clock' undeclared (first use this function)
1.cc:28: parse error before `;' token
1.cc:30: `myclock' undeclared (first use this function)

明明是一开始就定义了的类,后面却说没有定义的类
请问这是为什么?
发表于 2003-10-23 23:02:13 | 显示全部楼层
1.cc:15: `clock clock::settime(int, int, int)' and `void clock::settime(int = 0, int = 0, int = 0)' cannot be overloaded
仅返回值不同的函数不是重载,必需要参数不同才行,删掉一个。
其它错误没搞懂,贴一下源代码
 楼主| 发表于 2003-10-24 10:15:52 | 显示全部楼层
源代码如下:
#include<iostream>
using namespace std;

class clock
{
  public:
   void settime(int newh=0,int newm=0,int news=0);
   void showtime();
  private:
   int hour,minute,second;
}

void clock::settime(int newh,int newm,int news)
{
  hour=newh;
  mimute=newm;
  second=news;
}

inline void clock::showtime()
{
  cout<<hour<<":"<<minute<<":"<<second<<endl;
}

int main()
{
  clock myclock;
  cout<<"first time set and output:"<<endl;
  myclock.settime();
  myclock.showtime();
  cout<<"second time set and output:"<<endl;
  myclock.settime(8,30,30);
  myclock.showtime();
}

谢谢各位先
发表于 2003-10-24 11:11:22 | 显示全部楼层
最初由 scallion 发表
源代码如下:
#include<iostream>
using namespace std;

class clock
{
  public:
   void settime(int newh=0,int newm=0,int news=0);
   void showtime();
  private:
   int hour,minute,second;
}

void clock::settime(int newh,int newm,int news)
{
  hour=newh;
  mimute=newm;
  second=news;
}

inline void clock::showtime()
{
  cout<<hour<<":"<<minute<<":"<<second<<endl;
}

int main()
{
  clock myclock;
  cout<<"first time set and output:"<<endl;
  myclock.settime();
  myclock.showtime();
  cout<<"second time set and output:"<<endl;
  myclock.settime(8,30,30);
  myclock.showtime();
}

谢谢各位先


类声明时,有一处拼写错误,有一处隐性错误。
发表于 2003-10-24 11:23:24 | 显示全部楼层
class clock
{
......
}; //这里要有分号

mimute=newm; //minute,拼写错误,把n写成了m

改了以后还是编译通不过,我把类名字改成Clock就可以了,这个原因还不知道
发表于 2003-10-24 11:28:47 | 显示全部楼层
/usr/include/time.h:181: error: previous declaration of `clock_t clock()'
这个是重复使用,应该换个名字或者用名字空间把类包起来
 楼主| 发表于 2003-10-24 11:51:47 | 显示全部楼层
多谢版主的指点!
的确如你所说,把类名称第一个大写就可以了
前面的拼写错误和忘了点分号真是不好意思
再一次表示感谢,以后我会多加注意这些的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表