regarding class and objects

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  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.
co.x = co.x + ci.x[i];

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.

co.x = co.x + ci[i].x;
Thanks man. Got it right.
Topic archived. No new replies allowed.