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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器