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

Discussion in 'Mac Programming' started by Josh, Jan 29, 2006.

  1. macrumors 68000

    Josh

    Joined:
    Mar 4, 2004
    Location:
    State College, PA
    #1
    Hi,

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

    Here is my code:
    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.
     
  2. macrumors member

    SamMiller0

    Joined:
    Aug 17, 2004
    Location:
    San Jose, CA
    #2
    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:

    Code:
    const double pi = 3.141592654;
    
    and it should work fine
     
  3. macrumors member

    Joined:
    Jan 18, 2006
    Location:
    Finland
    #3
    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).
     
  4. thread starter macrumors 68000

    Josh

    Joined:
    Mar 4, 2004
    Location:
    State College, PA
    #4
    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.
     

Share This Page