PDA

View Full Version : c++ help: invalid arg. of 'unary *' ?




Josh
Jan 29, 2006, 06:57 PM
Hi,

I've wrote a small program to calculate the volume of a hollow metal "tube."

Here is my code:
#include <iostream>
#include <cmath>
using namespace std;

#define pi 3.141592654;

int main () {
float r,t,h,result; // radius, thickness, height

// the measurements
r = 5.0;
t = 3.0;
h = 10.0;

// doing the math
result = (pi*pow(r+t,2)*h)-(pi*pow(r,2)*h);

// return the result
cout << "The required amount of metal is: \n" << result << " cubic centimeters.\n";

return 0;

}


The result = line is the one giving me problems, but I don't see what's wrong with it :confused:

It gives me the invalid arguement of 'unary *' error, as well as some "expected ) before ;" and "expected ; before (" errors as well.

Thanks for any help, I really appreciate it.

*EDIT*

Fixed it somehow.



SamMiller0
Jan 31, 2006, 12:14 PM
You need to read Scott Meyers' book titled "Effictive C++", item #2 says "prefer consts, enums, and inlines to #define".

You should change your #define pi to:


const double pi = 3.141592654;


and it should work fine

fimac
Jan 31, 2006, 12:23 PM
Here is my code:
#define pi 3.141592654;

The result = line is the one giving me problems, but I don't see what's wrong with it :confused:


The trailing semi-colon is the source of your problem; #define'd words work rather like search-and-replace. Either remove the semi-colon or declare pi as a const (which IMHO is better).

Josh
Jan 31, 2006, 01:45 PM
Thanks for the info.

I'll have to look into that book; the only c++ book I currently have is our class textbook, which doubles as an intro to comp. science, so it's not very thorough I don' think.