Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我的程序失败了?_C++_Debugging - Fatal编程技术网

C++ 为什么我的程序失败了?

C++ 为什么我的程序失败了?,c++,debugging,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 <&l

我的代码如下:

#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;  
}
#包括
使用std::cout;
使用std::endl;
int next(int n)
{
返回n+1;
}
int main()
{
int next(int);//函数声明
int*fp=&next;
内部温度=10;
温度=(*fp)(温度);

cout函数指针的定义与普通指针不同

int (*fp)(int)
您的下一个函数在main中已经可见,无需重新声明它

#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;  
}
#包括
使用std::cout;
使用std::endl;
int next(int n)
{
返回n+1;
}
int main()
{
int(*next)(int);//函数指针
内部温度=10;
温度=下一个(温度);

coutnext是一个函数,其中as*fp是指向int not函数的指针

如何修复 这段代码不需要任何指针,只需编写

#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;  
}
#包括
使用std::cout;
使用std::endl;
int next(int n)
{
返回n+1;
}
int main()
{
内部温度=10;
温度=下一个(温度);
cout应该是:

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

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

@Ignacio Vazquez Abrams:为什么?如何修复?它被声明为指向
int
的指针。它需要声明为指向一个函数的指针,该函数接受
int
并返回
int
。但我记不起我的函数指针声明。