Reputation: 441
Just started programming C++, and stuck on the code below. On Ubuntu terminal I try to compile with
g++ -o circleArea circleArea.cpp
I get the error:
circleArea.cpp: In function ‘int main()’:
circleArea.cpp:14:14: error: invalid type argument of unary ‘*’ (have ‘double’)
area = PI * r * r;
^
The code is :
#include <iostream>
using namespace std;
#define PI 3.14159;
#define newLine "\n";
int main(void)
{
double r;
double area;
cout << "Please enter the radius : ";
cin >> r;
area = PI * r * r;
cout << "Area is " << area << " unit squares" << newLine;
}
If replace the line "area = PI * r * r" with "area = 3.14159 * r * r" I don't get any errors. What is the problem, can you help?
Thank you
Upvotes: 1
Views: 5428
Reputation: 23058
With #define PI 3.14159;
,
area = PI * r * r;
becomes
area = 3.14159; * r * r;
Note the extra ;
between 3.14159
and * r
. It's like
area = 3.14159;
* r * r;
and thus the first *
is treated as a unary dereference operator.
You should remove the semicolon in the end of macro definition.
Upvotes: 8