在C+中调用函数+; 我对整个C++编程的东西很陌生,我知道这是一个简单的解决方案,但我就是弄不懂! 我只想调用一个输出1+4的函数。 代码如下: #include <iostream> using namespace std; int func() { cout << 1 + 4; return 0; } int main() { int func(); } #包括 使用名称空间std; int func() { cout

在C+中调用函数+; 我对整个C++编程的东西很陌生,我知道这是一个简单的解决方案,但我就是弄不懂! 我只想调用一个输出1+4的函数。 代码如下: #include <iostream> using namespace std; int func() { cout << 1 + 4; return 0; } int main() { int func(); } #包括 使用名称空间std; int func() { cout,c++,function,C++,Function,您没有正确调用func()函数: int main() { // int func(); This line tries to declare a function which return int type. // it doesn't call func() func(); // this line calls func() function, it will ouput 5 return 0; } 只需按函数名调用函数即可 现在

您没有正确调用
func()
函数:

int main()
{
    // int func(); This line tries to declare a function which return int type.
    //             it doesn't call func()
    func();   // this line calls func() function, it will ouput 5
    return 0;
}

只需按函数名调用函数即可

现在主要是

int main (){
     int ans = func();// ans will catch the result which is return by the func();
     cout<<ans;
     return 0;
}
int main(){
int ans=func();//ans将捕获func()返回的结果;

coutWhat's symptoms?remove
int
调用函数时,现在它尝试声明一个新函数而不是调用它。直截了当!谢谢!甚至
返回func();
是,如果
func()
不是
void
谢谢,现在如果我在if语句中这样做,编译器会说“在“int”之前应该有一个主表达式。@Christoffer不会通过对答案的注释来“扩展”问题,如果您无法获得您的if,最好创建一个新问题。
int func(){
    return 1+4;// return 5 to main function.
}
int main (){
     int ans = func();// ans will catch the result which is return by the func();
     cout<<ans;
     return 0;
}