| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 812 人关注过本帖
标题:请教一道C++题目
只看楼主 加入收藏
durongjian
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-11-9
收藏
 问题点数:0 回复次数:4 
请教一道C++题目
#include <iostream>
using namespace std;
class Time
{
public:
    Time(int,int,int);
    int hour;
    int minute;
    int sec;
    void get_time(int);
    void output_minute();
};
Time::Time(int h,int m,int s)
{hour=h;minute=m;sec=s;}
void Time::get_time(int a)
{cout<<a<<","<<hour<<","<<minute<<","<<sec<<endl;}
void Time::output_minute()
{cout<<minute<<endl;}
void main()
{
    Time t1(10,13,56);
    int *p1=&t1.hour;
    cout<<*p1<<endl;
    t1.get_time(2008);
    Time *p2=&t1;
    p2->get_time(2008);
    void (Time::*p3)();
    p3=&Time::get_time(2008);(t1.*p3)();
    p3=&Time::output_minute;(t1.*p3)();
}
编译时出现如下错误,不明白,望高手指点
error C2352: 'Time::get_time' : illegal call of non-static member function
搜索更多相关主题的帖子: include public minute 
2008-11-11 19:24
alien73
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2008-9-18
收藏
得分:0 
get_time不是静态函数,怎么能用类名加域操作符调用呢。。。。
2008-11-11 19:30
durongjian
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-11-9
收藏
得分:0 
回复 2# 的帖子
非常感谢你的回复,但我还是不太懂,我如果把get_time函数的参数去掉后就可以了,为什么会这样,去掉后它仍然不是静态函数。
#include <iostream>
using namespace std;
class Time
{
public:
    Time(int,int,int);
    int hour;
    int minute;
    int sec;
    void get_time();
    void output_minute();
};
Time::Time(int h,int m,int s)
{hour=h;minute=m;sec=s;}
void Time::get_time()
{cout<<hour<<","<<minute<<","<<sec<<endl;}
void Time::output_minute()
{cout<<minute<<endl;}
void main()
{
    Time t1(10,13,56);
    int *p1=&t1.hour;
    cout<<*p1<<endl;
    t1.get_time();
    Time *p2=&t1;
    p2->get_time();
    void (Time::*p3)();
    p3=&Time::get_time;(t1.*p3)();
    p3=&Time::output_minute;(t1.*p3)();
}
2008-11-11 19:48
tls411323
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2008-10-26
收藏
得分:0 
2008-11-11 21:30
牙子
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2008-11-8
收藏
得分:0 
菜鸟帮你解题
因为出现函数指针赋值错误,就是语法错误;
p3=&Time::get_time(2008);本句话的意思是Time 类中有一个get_time(2008);的函数
其实Time 类中的函数是 get_time();
所以p3=&Time::get_time;就正确了;
 ***
呵呵本人刚自学C++到这里所有就大言不惭了,如果有错请认真批评;谢谢
2008-11-11 22:05
快速回复:请教一道C++题目
数据加载中...
 
   



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

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