开发者

Why 'q' and 'r' cannot be use as function?

开发者 https://www.devze.com 2023-03-26 07:15 出处:网络
So I\'m trying to return the perimeter and area in int main, but g++ keeps throwing error messages. Please help in explanation of error. Thanks.

So I'm trying to return the perimeter and area in int main, but g++ keeps throwing error messages. Please help in explanation of error. Thanks.

Source:

  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     // peri开发者_JAVA百科meter
 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 }

Output:

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


Because you have defined q and r as doubles, then you are trying to 'call' them like a function with your ()s

Edit

Also, like the other answers state, depending on what you want to do - you either need to define q and r as functions (like you have done with area and perimeter or you need to call your two existing functions on x and y e.g. area(x, y)


q and r are declared as variables of type double.

You want to use perimeter(x,y) and area(x,y) as:

// perimeter
cout << "The perimeter is: " << perimeter(x, y) << endl;

// area
cout << "The area is: " << area(x, y) << endl;


You have declared q and r as variables. You cannot use them as functions because they are not functions. For r(x,y) to be valid code, you would need to declare somewhere double r(double x, double y) { ....} and remove the r variable declaration.

Please read a book or something on the basics of the language.


// 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;
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消