qq_40594061 2017-12-18 17:43 采纳率: 66.7%
浏览 1903
已结题

关于函数作参数的问题,请大佬指教

这个代码是把Output函数作为一个地址来做的,不知道为什么编译的
时候一直给我报错,可是书上明明可以的啊
错误如下:C:\Users\ADMINI~1\AppData\Local\Temp\ccxuRVz9.o 函数作参数.cpp:(.rdata$.refptr._ZN3ICS6OutputE[.refptr._ZN3ICS6OutputE]+0x0): undefined reference to `ICS::Output'
C:\Users\Administrator\Desktop\collect2.exe [Error] ld returned 1 exit status

#include
using namespace std;

class ICS
{
private:
int a;
int b;
static void (*Output)(int a);
public:
ICS();
void show(void(*thevisit)(int))
{
Output = thevisit;
Output(a);
}
void show1()
{
show(output);
}

};

ICS::ICS()
{
cout << "the sum \n";

cin >> a;
cout << a;
}

void ICS::output(int a)
{
cout << "the number is " << a << endl;
}

int main()
{
ICS I;
I.show1();

return 0;

}

  • 写回答

2条回答 默认 最新

  • threenewbee 2017-12-18 18:36
    关注

    static void (*Output)(int a);
    注意Output的O是大写的

     #include<iostream>
    using namespace std;
    
    class ICS
    {
        private:
            int a;
            int b;
            static void (*Output)(int a);
        public:
            ICS();
            void show(void(*thevisit)(int))
            { 
                Output = thevisit;
                Output(a);
            }
            void show1()
            {
                show(Output);
            }
    void output(int a);
    
    };
    
    ICS::ICS()
    {
        cout << "the sum \n";   
        cin >> a;
        cout << a;
    }
    
    void ICS::output(int a)
    {
        cout << "the number is " << a << endl;
    }
    
    
    int main()
    {
        ICS I;
        I.show1();
    
        return 0;
    }
    

    这么写可以编译通过。

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名