gpt4 book ai didi

c++ - C++中的函数帮助

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:24:03 28 4
gpt4 key购买 nike

我试图在 C++ 中创建一个非常简单的函数,但是我一直收到“链接错误”。

我的代码:

#include <iostream>

using namespace std ;


int fun(int,int);

main(){
int width,height,w,h,mult;

cin>>width;
cin>>height;

mult = fun(width,height);

int fun(int w,int h);{
w * h ;
}

cout << mult ;

}

错误:

[Linker error] undefined reference to `fun(int, int)' 
ld returned 1 exit status

最佳答案

在任何地方都没有fun(int, int) 的实现。实现它的模块应该与此链接。或者你应该在上面的模块中编写函数,也许在原型(prototype)所在的位置。

似乎中途定义函数的尝试失败了:

int fun(int w,int h);{
w * h ;
}

这实际上做了什么,它(再次)声明有一些函数 int fun():这是一个原型(prototype)。然后是一个表达式 w * h,它仍在函数 main 中,它被计算但没有对结果做任何事情。

关于c++ - C++中的函数帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3902219/

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