#include<iostream>
#define pi 3.141592654
usingnamespace std;
class Triangle
{
int base, height;
public:
friend Triangle set_value(int,int);
int area()
{
return 5e-1*base*height;
}
};
Triangle set_value(int a, int b)
{
Triangle lol;
lol.base = a;
lol.height = b;
return lol;
}
class Circle
{
float radius;
public:
friend Circle set_value(int a)
{
Circle n;
n.radius = a;
}
float area();
};
float Circle :: area()
{
return pi*radius*radius;
}
int main()
{
Triangle tri;
Circle cir;
int n,m,r;
cout << "Triangle size = ";
cin >> n >> m;
cout << "Circle size = ";
cin >> r;
system("cls");
tri = set_value(n,m);
cir = set_value(r);
cout << "Triangle area = " << tri.area();
cout << "\nCircle Area = " << cir.area();
}
|53|error: too few arguments to function 'Triangle set_value(int, int)'|
According to the compiler, that is the error, but cir is a Circle class and I have set the set_value in Circle class to have only have 1 variable as parameterfriend Circle set_value(int a), so why is it error?
This is an attempt to call the function Triangle set_value(int a, int b)
As you can see, the function takes another int.
I have set the set_value in Circle class to have only have 1 variable
That's true, but you're not calling the class function Circle::set_value(int), are you? You're calling the function Triangle set_value(int a, int b)
If you want to call a class function, you have to call it on an object of that class.
1 2 3
Circle cir; // Create an object of class Circle
cir.set_value(5); // Call the function Circle::set_value(int) on the object of class Circle
set_value(6, 7); // Call the non-class function Triangle set_value(int a, int b)
@Repeater: there is no member function Circle::set_value(int)
notice that it is declared as friend and defined inline, it's a free (global) function, not a member function
as to why the compiler can't see it, the lookup method changes https://stackoverflow.com/a/382077 (the example uses an operator, but the same may be said about a function)
It makes the operator not visible to normal lookup. The only way you can call it is using argument dependent look-up. This will keep the namespace free of lots of operator declarations visible normally.
as to how to solve it, well, remove such a crappy friend function
you may use a constructor, that's what they are for.