为什么我的编程失败?

时间:2010-12-29 13:28:23

标签: c++ debugging

我的代码如下:

#include <iostream>

using std::cout;
using std::endl;

int next(int n)
{
    return n + 1;
}

int main()
{
    int next(int);  // function declaration
    int *fp = &next;

    int temp = 10;
    temp = (*fp)(temp);
    cout << temp << endl;

    return 0;  
}

需要调试编译器点int *fp = &next;,但是,我没有发现这句话有什么问题。 你能告诉我吗?谢谢你的时间~~

5 个答案:

答案 0 :(得分:8)

函数指针的定义不像普通指针

int (*fp)(int)

你的下一个功能已在main中显示,无需重新声明

答案 1 :(得分:4)

应该是:

int (*fp)(int);  
fp = next;

答案 2 :(得分:3)

next是一个函数,因为* fp是指向int而不是函数的指针。

如何修复

你不需要这个代码的任何指针。你可以写

#include <iostream>

using std::cout;
using std::endl;

int next(int n)
{
    return n + 1;
}

int main()
{
    int temp = 10;
    temp = next(temp);
    cout << temp << endl;

    return 0;  
}

答案 3 :(得分:1)

尝试这样:

#include <iostream>

using std::cout;
using std::endl;

int next(int n)
{
    return n + 1;
}

int main()
{
    int (*next)(int);   // function POINTER

    int temp = 10;
    temp = next(temp);
    cout << temp << endl;

    return 0;  
}

答案 4 :(得分:1)

指向函数的指针不是指向整数的指针。

相关问题