| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 666 人关注过本帖
标题:为什么我定义声明了add()函数,可执行程序的时候程序提示add()函数未定 ...
只看楼主 加入收藏
lianjiecuowu
Rank: 3Rank: 3
来 自:安徽
等 级:论坛游侠
帖 子:152
专家分:107
注 册:2011-5-20
结帖率:85.71%
收藏
已结贴  问题点数:20 回复次数:2 
为什么我定义声明了add()函数,可执行程序的时候程序提示add()函数未定义呢?求解释,有点晕,想不出原因啊,拜托大家
#include<iostream>
using namespace std;
class Time
{
      private:
      int hours,minutes,seconds;
      public:
      Time(void){};
      Time(int h,int m,int s);
      Time(Time &t);
      ~Time(){cout<<"the end"<<endl;}
      void settime();
      int  gettime();
      void add(Time&t1,Time&t2);
};
Time::Time(int h,int m,int s)
{
      hours=h;minutes=m;seconds=s;
      cout<<"现在的时间为:"<<hours<<":"<<minutes<<":"<<seconds<<endl;
}
Time::Time(Time &t)
{
      hours=t.hours;minutes=t.minutes;seconds=t.seconds;
      cout<<"现在的时间为:"<<hours<<":"<<minutes<<":"<<seconds<<endl;
}
void Time::settime()
{
     int h,m,s;
     cin>>h>>m>>s;
     hours=h;
     minutes=m;
     seconds=s;
}
int Time::gettime()
{
   cout<<"现在的时间为:"<<hours<<":"<<minutes<<":"<<seconds<<endl;
   return 0;
}
void Time::add(Time&t1,Time&t2)
{
     Time total;
     const int perminutes=60;
     const int perhours=60;
     total.seconds=(t1.seconds+t2.seconds)%perminutes;
     total.minutes=(t1.minutes+t2.minutes+(t1.seconds+t2.seconds)/perminutes)%perhours;
     total.hours=(t1.hours+t2.hours+(t1.minutes+t2.minutes+(t1.seconds+t2.seconds)/perminutes)/perhours);
     cout<<"现在的时间为:"<<total.hours<<":"<<total.minutes<<":"<<total.seconds<<endl;

     
}
int main()
{
    Time a(5,5,23);
   
    cout<<"请设置当前的时间:"<<endl;
    a.settime();
    a.gettime();
    cout<<endl<<endl;
    Time b(6,4,34);
    add(a,b);         //error C2065: 'add' : undeclared identifier
    system("pause");
    return 0;
}
搜索更多相关主题的帖子: private 
2011-06-09 13:02
lianjiecuowu
Rank: 3Rank: 3
来 自:安徽
等 级:论坛游侠
帖 子:152
专家分:107
注 册:2011-5-20
收藏
得分:0 
#include<iostream>
using namespace std;
class Time
{
      private:
      int hours,minutes,seconds;
      public:
      Time(void){};
      Time(int h,int m,int s);
      Time(Time &t);
      ~Time(){cout<<"the end"<<endl;}
      void settime();
      int  gettime();
      void add(Time&t1,Time&t2);
};
Time::Time(int h,int m,int s)
{
      hours=h;minutes=m;seconds=s;
      cout<<"现在的时间为:"<<hours<<":"<<minutes<<":"<<seconds<<endl;
}
Time::Time(Time &t)
{
      hours=t.hours;minutes=t.minutes;seconds=t.seconds;
      cout<<"现在的时间为:"<<hours<<":"<<minutes<<":"<<seconds<<endl;
}
void Time::settime()
{
     int h,m,s;
     cin>>h>>m>>s;
     hours=h;
     minutes=m;
     seconds=s;
}
int Time::gettime()
{
   cout<<"现在的时间为:"<<hours<<":"<<minutes<<":"<<seconds<<endl;
   return 0;
}
void Time::add(Time&t1,Time&t2)
{
     Time total;
     const int perminutes=60;
     const int perhours=60;
     total.seconds=(t1.seconds+t2.seconds)%perminutes;
     total.minutes=(t1.minutes+t2.minutes+(t1.seconds+t2.seconds)/perminutes)%perhours;
     total.hours=(t1.hours+t2.hours+(t1.minutes+t2.minutes+(t1.seconds+t2.seconds)/perminutes)/perhours);
     cout<<"现在的时间为:"<<total.hours<<":"<<total.minutes<<":"<<total.seconds<<endl;

     
}
int main()
{
    Time T;
    Time a(5,5,23);
   
    cout<<"请设置当前的时间:"<<endl;
    a.settime();
    a.gettime();
    cout<<endl<<endl;
    Time b(6,4,34);
    cout<<endl<<endl;
    T.add(a,b);
    system("pause");
    return 0;
}
我知道啦,额。不好意思,没看出来额.........真是

Sharp your mind!
2011-06-09 13:06
棉雨
Rank: 5Rank: 5
等 级:职业侠客
帖 子:174
专家分:368
注 册:2011-4-22
收藏
得分:20 
回复 楼主 lianjiecuowu
#include<iostream>
using namespace std;
class Time
{
      private:
      int hours,minutes,seconds;
      public:
      Time(void){};
      Time(int h,int m,int s);
      Time(Time &t);
      ~Time(){cout<<"the end"<<endl;}
      void settime();
      int  gettime();
      void add(Time&t1,Time&t2);
};
Time::Time(int h,int m,int s)
{
      hours=h;minutes=m;seconds=s;
      cout<<"现在的时间为:"<<hours<<":"<<minutes<<":"<<seconds<<endl;
}
Time::Time(Time &t)
{
      hours=t.hours;minutes=t.minutes;seconds=t.seconds;
      cout<<"现在的时间为:"<<hours<<":"<<minutes<<":"<<seconds<<endl;
}
void Time::settime()
{
     int h,m,s;
     cin>>h>>m>>s;
     hours=h;
     minutes=m;
     seconds=s;
}
int Time::gettime()
{
   cout<<"现在的时间为:"<<hours<<":"<<minutes<<":"<<seconds<<endl;
   return 0;
}
void Time::add(Time&t1,Time&t2)
{
     Time total;
     const int perminutes=60;
     const int perhours=60;
     total.seconds=(t1.seconds+t2.seconds)%perminutes;
     total.minutes=(t1.minutes+t2.minutes+(t1.seconds+t2.seconds)/perminutes)%perhours;
     total.hours=(t1.hours+t2.hours+(t1.minutes+t2.minutes+(t1.seconds+t2.seconds)/perminutes)/perhours);
     cout<<"现在的时间为:"<<total.hours<<":"<<total.minutes<<":"<<total.seconds<<endl;
     
}
int main()
{
    Time a(5,5,23);
    Time total;
    cout<<"请设置当前的时间:"<<endl;
    a.settime();
    a.gettime();
    cout<<endl<<endl;
    Time b(6,4,34);
    total.add( a,b);         //error C2065: 'add' : undeclared identifier(成员函数的调用前面要写上对象名,形如:对象名.成员函数(参数),add是Time的成员函数,所以,要先定义另一个对像,然后再调用add函数。)
    system("pause");
    return 0;
}
2011-06-09 19:41
快速回复:为什么我定义声明了add()函数,可执行程序的时候程序提示add()函数 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.069294 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved