Overloading the [] operator

  • A+
Category:Languages

I am a beginner in C++. I am learning on how to overload operators. I have created a class Complex that represents complex numbers and methods for complex arithmetic and a class ComplexArray that represents fixed-length arrays of elements in complex vector space C.

I get compiler errors, that it is unable to find the correct form of operator[]. However, I searched the internet and I am unable to rectify the error. Any hints/tips in the right direction would be of tremendous help.

Severity Code Description Project File Line Suppression State
Error C2676 binary '[': 'const ComplexArray' does not define this operator or a conversion to a type acceptable to the predefined operator ComplexArrays c:/users/quasa/source/repos/complexarrays/complexarrays/testcomplexarray.cpp 7

Here is my code:

TestComplexArray.cpp

#include <iostream> #include "ComplexArray.h"  Complex ComplexSum(const ComplexArray& cArray, int size) {     Complex sum = cArray[0];     for (int i = 1; i < size; i++)     {         sum = sum + cArray[i];     }     return sum; }  Complex ComplexProduct(const ComplexArray& cArray, int size) {     Complex product = cArray[0];     for (int j = 1; j < size; j++)     {         product = product * cArray[j];     }     return product; }  int main() {     char ch;      const int size = 5;     ComplexArray cArray(size);      for (int i = 0; i < size; i++)     {         cArray[i] = Complex((double)(i + 1), 0);         std::cout << cArray[i];     }      Complex sum = ComplexSum(cArray, size);     Complex product = ComplexProduct(cArray, size);      std::cout << "Sum = " << sum << std::endl;     std::cout << "Product = " << product << std::endl;      std::cin >> ch;     return 0; } 

ComplexArray.h

class ComplexArray { private:     Complex* complexArr;     int size;      ComplexArray(); public:     //Constructors and destructors     ComplexArray(int size);     ComplexArray(const ComplexArray& source);     virtual ~ComplexArray();      //Range for the complexArr     int MaxIndex() const;      //Overload the indexing operator     const Complex& operator [](int index) const;     Complex& operator [](int index); }; 

ComplexArray.cpp

#include "Complex.h" #include "ComplexArray.h"  ComplexArray::ComplexArray(int s) {     size = s;     complexArr = new Complex[size]; }  ComplexArray::ComplexArray(const ComplexArray& source) {     //Deep copy source     size = source.size;      complexArr = new Complex[size];      for (int i = 0; i < size; i++)     {         complexArr[i] = source.complexArr[i];     } }  ComplexArray::~ComplexArray() {     delete[] complexArr; }  int ComplexArray::MaxIndex() const {     return (size - 1); }  /* c1.operator[](int index) should return a reference to the Complex object, because there are two possible cases.  Case 1: Complex c = complexArray[3];  Case 2: complexArray[3] = c;  In the second case, complexArray[3] is an lvalue, so it must return a Complex object  by reference, so that it can be assigned to. */  const Complex& ComplexArray::operator[] (int index) const {     return complexArr[index]; }  Complex& ComplexArray::operator[](int index) {     return complexArr[index]; } 

Complex.h

#include <iostream>  class Complex { private:     double x;     double y;     void init(double xs, double ys); //Private helper function  public:     //Constructors and destructors     Complex();     Complex(const Complex& z);     Complex(double xs, double ys);     virtual ~Complex();      //Selectors     double X() const;     double Y() const;      //Modifiers     void X(double xs);     void Y(double ys);      //Overload binary +, = and * operators     Complex operator + (const Complex& z);     Complex& operator = (const Complex& z);     Complex operator * (const Complex& z) const;      //Overload unary - operator     Complex operator -() const;      friend Complex operator * (const double alpha, const Complex& z);     friend Complex operator * (const Complex& z, const double beta);      //Overload << operator     friend std::ostream& operator << (std::ostream& os, const Complex& z);      //A complex function f(z)=z^2     Complex square(); }; 


As you have all pointed out - I was missing the forward definition of a #include.

Complex.cpp has the header

#include "Complex.h" 

ComplexArray.h has the header

#include "Complex.h" 

ComplexArray.cpp has the header

#include "ComplexArray.h" 

TestComplexNumbers.cpp has the header

#include <iostream> #include "ComplexArray.h" 

My compile-time errors have been resolved.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: