Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading the [] operator

Tags:

c++

oop

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();
};
like image 976
Quasar Avatar asked May 24 '18 07:05

Quasar


People also ask

Can [] operator be overloaded?

Subscripting [] Operator Overloading in C++ The subscript operator [] is normally used to access array elements. This operator can be overloaded to enhance the existing functionality of C++ arrays.

Can we overload [] operator in C++?

In the following example, a class called complx is defined to model complex numbers, and the + (plus) operator is redefined in this class to add two complex numbers. where () is the function call operator and [] is the subscript operator. You cannot overload the following operators: .

What should be the function signature for the overloading [] operator?

The function signature should be like this: // Add this instance's value to other, and return a new instance // with the result. const MyClass MyClass::operator+(const MyClass &other) const { MyClass result = *this; // Make a copy of myself.

What is operator overloading with example?

In C++, we can change the way operators work for user-defined types like objects and structures. This is known as operator overloading. For example, Suppose we have created three objects c1 , c2 and result from a class named Complex that represents complex numbers.


1 Answers

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.

like image 134
Quasar Avatar answered Sep 20 '22 04:09

Quasar