Friday, July 31, 2009

My c++ program is a basic one but i get this error "error C2659: '=' : function as left operand"

i get this error on the last line of code before the final closed brace


here is the whole program (i want to average together 3 numbers)





#include %26lt;iostream%26gt;


#include %26lt;math.h%26gt;


using namespace std;


double avg (double a, double b, double c);


int main ()


{


double a, b, c;


cout%26lt;%26lt;"Enter a Number that is greater than zero:";


cin%26gt;%26gt;a;


cout%26lt;%26lt;"Enter another Number greater than zero:";


cin%26gt;%26gt;b;


cout%26lt;%26lt;"Enter the Final number:";


cin%26gt;%26gt;c;


cout%26lt;%26lt;"The average of the three numbers is:";


cout%26lt;%26lt;avg;





char z;


cin%26gt;%26gt; z;


return 0;


}





double avg (double a, double b, double c)


{


avg=(a+b+c)/3;


}

My c++ program is a basic one but i get this error "error C2659: '=' : function as left operand"
There are a couple of problems with this code. first the line


cout %26lt;%26lt; avg;


Actually prints out the function pointer to avg(). Second in the function avg() the identifier avg is actually the pointer to the function avg() which is the only identifer with that name in the scope since it is not redefined inside of the function. avg()





double avg(double a, double c, double c){


  return (a + b + c)/3;


}





This would also work.





double avg(double a, double c, double c){


   double avg = (a + b + c)/3;


  return avg;


}
Reply:the easiest way to fix it is to put your disk to your computer and let it run to do a repair it will ask you what you want to do, or do a restore it back a week or two when you didn't have this problem
Reply:Above answers are right - you cannot say "avg = ***" on the last line in C++ (or C). You need "return (a + b + c)/3;".





There are 2 types of things that look like variables in C. They are called "lvalues" and "rvalues", but "variables" and "constants" might be better terms. Both hold a value, but an rvalue's value is assigned at compile time and cannot be changed. ("rvalue" is short for "right value" because it can only be on the right of an assignment. "lvalue" is "left value" because it can be on the left of an assignment)





In C a function name is actually a pointer to the function - this can be valuable if you want to include a function in a table. As an example, if you write a calculater program you might want to set up a table mapping from function character to function:


{


{'+', plus};


{'-', minus};


{'*', times};


{'/', divide};


};





could be looped through to call the proper function for each command. The compiler assigns the value when it compiles the preogram. (actually, the loader assigns the value when the program is run, but it follows the compiler's instructions)





An lvalue can have its assignment made at runtime. Most variables are lvalues. For them you can make an assignment.





In your example, avg is an rvalue because it is a function address, and so cannot be assigned to.
Reply:i gotta learn more about c++ srry.
Reply:I managed to get it running...what I did was I added a new variable (called avg1) and gave your function a return value (if a function is not void it needs to return something), and renamed all references to avg to avg1.





The reason why you were getting that error was because (the computer thinks) you were tiring to preform an aggregate (an operation that addresses the function as one item) operation, which is not allowed in C++ unless you do some type of overloading, regardless please take a look at my solution and learn from it. Best of luck in the programming world, its really fun!!!





#include %26lt;iostream%26gt;


#include %26lt;math.h%26gt;


using namespace std;


double avg (double a, double b, double c,double avg1);


int main ()


{


double a, b, c,avg1;


avg(a,b,c,avg1);


cout%26lt;%26lt;"Enter a Number that is greater than zero:";


cin%26gt;%26gt;a;


cout%26lt;%26lt;"Enter another Number greater than zero:";


cin%26gt;%26gt;b;


cout%26lt;%26lt;"Enter the Final number:";


cin%26gt;%26gt;c;


cout%26lt;%26lt;"The average of the three numbers is:";


cout%26lt;%26lt;avg1;





char z;


cin%26gt;%26gt; z;


return 0;


}





double avg (double a, double b, double c,double avg1)


{


avg1=(a+b+c)/3;


return 0;


}
Reply:You use function name as a left operand in assignment:


double avg (double a, double b, double c)


{


avg=(a+b+c)/3;


}


It is valid in some programming languages but not in C/C++. Simply do the following:


double avg (double a, double b, double c)


{


return (a+b+c)/3;


}
Reply:maybe i'm reading this wrong (my C is rusty) but...


double avg (double a, double b, double c)


{


avg=(a+b+c)/3;


}





you've declared a method as avg, and a variable as avg.





double avg (double a, double b, double c)


{


double average;


average=(a+b+c)/3;


}
Reply:when u declare a int, double, char etc you have to return something





double avg(double a, double b, double c)


{


double t;


t = (a+b+c)/3


return t;


}





then change


cout%26lt;%26lt;"The average of the three numbers is:";


cout%26lt;%26lt;avg(a,b,c);


No comments:

Post a Comment