Reputation: 52941
I'm trying to create a very simple function in c++ however I keep getting a "Link error".
My code:
#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 ;
}
The error:
[Linker error] undefined reference to `fun(int, int)'
ld returned 1 exit status
Upvotes: 1
Views: 107
Reputation: 282875
Ack...so many things wrong with that. Should be something like this:
#include <iostream>
using namespace std ;
int fun(int, int);
void main(){
int width,height,mult;
cin >> width;
cin >> height;
mult = fun(width, height);
cout << mult << endl;
}
int fun(int w, int h) {
return w*h;
}
(Been awhile since I touched C++)
Upvotes: 2
Reputation: 79185
You are declaring a global function fun, and you define it inside main. You should declare it outside, or remove the external declaration
Upvotes: 1
Reputation: 57774
There is no implementation of fun(int, int)
anywhere. The module which implements it should be linked in with this. Or you should write the function in the module above, perhaps where the prototype is.
It appears there is a failed attempt to define the function midway:
int fun(int w,int h);{
w * h ;
}
What this actually does it declare (again) that there is some function int fun()
: that is a prototype. Then there is an expression w * h
, still inside function main which is evaluated but nothing is done with the result.
Upvotes: 6