所以我正在尝试返回int main
中的边界和区域,但g++
不断抛出错误消息。请帮助解释错误。感谢。
1 // This program takes in two values and returns the perimeter and the area of the values taken.
2
3 #include<iostream>
4 using namespace std;
5
6 double perimeter (double a, double b);
7 double area (double a, double b);
8
9 int main ()
10 {
11 // initialization
12 double x, y, r, q;
13
14 // inputs
15 cout << "Please enter the first value (no units): " << endl;
16 cin >> x;
17 cout << "Please enter the second value (no units): " << endl;
18 cin >> y;
19
20 // perimeter
21 cout << "The perimeter is: " << r << endl;
22 r(x, y);
23
24 // area
25 cout << "The area is: " << q << endl;
26 q(x, y);
27
28 return 0;
29 }
30
31 double perimeter (double a, double b)
32 {
33 2.0 * (a + b);
34 return 0;
35 }
36
37 double area (double a, double b)
38 {
39 a * b;
40 return 0;
41 }
x@xLinux:~$ g++ -Wall rectangle.cpp
rectangle.cpp: In function ‘int main()’:
rectangle.cpp:22:11: error: ‘r’ cannot be used as a function
rectangle.cpp:26:11: error: ‘q’ cannot be used as a function
rectangle.cpp: In function ‘double perimeter(double, double)’:
rectangle.cpp:33:18: warning: statement has no effect
rectangle.cpp: In function ‘double area(double, double)’:
rectangle.cpp:39:10: warning: statement has no effect
答案 0 :(得分:7)
因为您已将q
和r
定义为双打,所以您试图像对待()
s
修改强>
此外,与其他答案一样,根据您要执行的操作,您需要将q
和r
定义为函数(就像您使用area
和{ {1}}或者您需要在perimeter
和x
上调用您现有的两个函数,例如y
答案 1 :(得分:5)
q
和r
被声明为double
类型的变量。
您希望将perimeter(x,y)
和area(x,y)
用作:
// perimeter
cout << "The perimeter is: " << perimeter(x, y) << endl;
// area
cout << "The area is: " << area(x, y) << endl;
答案 2 :(得分:3)
您已将q
和r
声明为变量。您不能将它们用作函数,因为它们不是函数。要使r(x,y)
成为有效代码,您需要在某处double r(double x, double y) { ....}
声明并删除r
变量声明。
请阅读有关该语言基础知识的书籍或其他内容。
答案 3 :(得分:2)
// This program takes in two values and returns the perimeter and the area of the values taken.
#include<iostream>
using namespace std;
double perimeter (double a, double b);
double area (double a, double b);
int main ()
{
// initialization
double x, y;
// inputs
cout << "Please enter the first value (no units): " << endl;
cin >> x;
cout << "Please enter the second value (no units): " << endl;
cin >> y;
// perimeter
cout << "The perimeter is: " << perimeter(x, y) << endl;
// area
cout << "The area is: " << area(x, y) << endl;
return 0;
}
double perimeter (double a, double b)
{
return 2.0 * (a + b);
}
double area (double a, double b)
{
return a * b;
}