Can somebody help me out here. I want to add complex expressions as many as per the request of user. I am trying to send array of objects through function arguement.
class complex
{
float x;
float y;
public:
void Input (float a,float b)
{ x = a; y = b; }
complex Sum (complex,int);
void Display (complex);
};
complex complex :: Sum (complex ci[], int q) // q is the no. of exp.s to be added
{
float x;
float y;
complex co;
co.x = co.y = 0;
for (int i=1;i<=q;i++)
{
co.x = co.x + ci.x[i];
co.y = co.y + ci.y[i];
}
return (co);
}
It generates error :prototype for `complex complex::Sum(complex*, int)' does not match any in class `complex'
:`x' is not a type. request for member of non-aggregate type before '[' token
(same for y)
You defined the Sum function parameters as a single complex instance and an int, but you're trying to implement the function using an array of complex and an in int. The function signatures must match.
Thanks for that one.
I changed the function declaration to complex "Sum(complex c[], int);"
But again the error:"`x' is not a type. request for member of non-aggregate type before '[' token".
still persists.
Looks like ci is the array of complex objects and x (and y in the next line) are data members of type float? If you're trying to iterate through the different elements of the complex array and pull each x value, then the [i] would be next to ci, not x.