gpt4 book ai didi

c++ - 函数指针演示

转载 作者:太空狗 更新时间:2023-10-29 23:34:35 25 4
gpt4 key购买 nike

检查下面的代码

int add(int a, int b)
{
return a + b;
}

void functionptrdemo()
{
typedef int *(funcPtr) (int,int);
funcPtr ptr;
ptr = add; //IS THIS CORRECT?
int p = (*ptr)(2,3);
cout<<"Addition value is "<<p<<endl;
}

在我尝试将函数分配给具有相同函数签名的函数 ptr 的地方,它显示编译错误为 error C2659: '=' : function as left operand

最佳答案

很可能您打算写的不是:

typedef int *(funcPtr) (int,int);

但是:

typedef int (*funcPtr) (int,int);

关于c++ - 函数指针演示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2590553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com