16

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();
};
18
  • 3
    Note that your ComplexArray type uses your Complex type, but the ComplexArray.h header does not #include the Complex.h header. I haven't tested, but I am very suspicious about this.
    – BoBTFish
    May 24, 2018 at 7:09
  • 10
    Why the drive-by downvote? This is actually a very well structured question, certainly for someone pretty new. All the detail is there, including the error message. Could be a bit more minimal I suppose, but the code isn't really huge.
    – BoBTFish
    May 24, 2018 at 7:11
  • 1
    your ComplexProduct and ComplexSum should return Complex value
    – Deb S
    May 24, 2018 at 7:19
  • 1
    TestComplexArray.cpp would have undefined class problem because you included Complex.h after ComplexArray. Can be be fixed by a forward definition (unless you forgot to show one) May 24, 2018 at 7:21
  • 2
    Now this is on HNQ, congratulations. About BoBTFish's comment about "minimal" above, see minimal reproducible example.
    – user202729
    May 24, 2018 at 16:28

3 Answers 3

11

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.

3
  • 5
    Nice answer. The most important point is that you must declare Complex before you use it inside ComplexArray. This effectively means you must remember to #include "Complex.h" at the top of ComplexArray.h.
    – BoBTFish
    May 24, 2018 at 7:38
  • 1
    sometimes just including into header causes circular includes which usually results in compiler's malfunction. there are cases when you can use just an incomplete type. May 24, 2018 at 7:56
  • Gotcha! I will do this as a best practice then.
    – Quasar
    May 24, 2018 at 8:01
7

I don't think the error comes from operator[], as you can see in the function:

Complex ComplexSum(const ComplexArray& cArray, int size)
{
    Complex sum = cArray[0];
    for (int i = 1; i < cArray.MaxIndex(); i++)
    {
        sum = sum + cArray[i];
    }
}

You don't return a result. That's fatal.

1
  • 6
    Absolutely a big problem, but not actually the one causing the error message in the question. Worth leaving the answer though, but maybe clarify the first sentence.
    – BoBTFish
    May 24, 2018 at 7:40
3

ComplexArray depends on Complex but order of includes doesn't look right

#include "ComplexArray.h"
#include "Complex.h"

You have to forward-declare Complex before ComplexArray

 class Complex; 

Code fails at \testcomplexarray.cpp line 7 which is

 Complex sum = cArray[0];

It looks like you have problem with ctors of Complex. Be sure that you have NOT defined such:

 Complex(Complex& v);  // that's bad. it prevents to use copy constructor

If you need copy ctor for some inconceivable reason, it always should look so:

 Complex(const Complex& v);
4
  • I added the header file for [code]Complex.h[/code] for clarity. Hang on, I am trying summing and multiplying the complex numbers.
    – Quasar
    May 24, 2018 at 7:54
  • @Quasar yeah, that looks fine. to be honest I don't see why you need copy constructor, that looks like Complex written in Straustrup's version of C++. are you doing some error checking in there, e.g. in inti()? If copy constructor just copies fields, then Complex(const Complex& z) = default; is enough May 24, 2018 at 8:00
  • I see your point. The book that I am using declares all constructors explicitly. amazon.in/…
    – Quasar
    May 24, 2018 at 8:06
  • @Quasar ah..and it's just a little old.. unless you use old compiler. C++11 had simplified a lot of things. Although I would say that knowing C++98 style doesn't harm, there are still areas for it. Concern may be that if that book relies on C++0x (03), it may mention additions to language which where removed\changed in C++11. May 24, 2018 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.