C++ Operator Overload Error
<p>I'm trying to create my own version of an array called a safearray, to test my knowledge of operator overloading and creating proper class's and such.</p>

<p>I'm encountering two errors.</p>

<p>SafeArray.h:11:15: error: ‘const int SafeArray::operator’ cannot be overloaded</p>

<p>SafeArray.h:10:10: error: with ‘int&amp; SafeArray::operator’</p>

<p>My code is split between three files.</p>

<p>Main.cpp</p>

<pre><code>#include &lt;cstdlib&gt;
#include &lt;iostream&gt;

#include "SafeArray.h"

using namespace std;

int main(int argc, char** argv) {

    SafeArray a(10); // 10 integer elements

    for (int i = 0; i &lt; a.length(); i++) {
        cout &lt;&lt; i &lt;&lt; " " &lt;&lt; a[i] &lt;&lt; "s" &lt;&lt; endl; // values initialise to 0
    }

    cout &lt;&lt; endl &lt;&lt; a[1]; // Program exits here.

    a[3] = 42;
    cout &lt;&lt; a[3];
    a[10] = 10;
    cout &lt;&lt; a[10];
    a[-1] = -1; // out-of-bounds is "safe"?

    SafeArray b(20); // another array

    b = a; // array assignment

    for (int i = 0; i &lt; b.length(); i++) {
        cout &lt;&lt; b[i] &lt;&lt; endl; // values copied from a
    }

    return 0;
}
</code></pre>

<p>SafeArray.h</p>

<pre><code>#ifndef SAFEARRAY_H
#define SAFEARRAY_H

class SafeArray {
public:
    SafeArray(int);              // int variable will be the array size
    int length();
    int boundsCheck(int y);       // constructor will call this function
//    const SafeArray operator= (const SafeArray&amp;);
    int&amp; operator[] (int y);
    const int operator [] (const int y); // you need this one too.

    SafeArray &amp;operator=(SafeArray rhs) { 
    std::swap(array, rhs.array);
    std::swap(length_, rhs.length_);
        }

    SafeArray(SafeArray const &amp;other);
    ~SafeArray();
private:
    int length_;
    int *array;
    //int array[];
};

#endif  /* SAFEARRAY_H */
</code></pre>

<p>SafeArray.cpp</p>

<pre><code>#include "SafeArray.h"
#include &lt;iostream&gt;

SafeArray::SafeArray(int x) {
    length_ = x;
    array = new int[length];
    for (int i = 0; i &lt; length_; i++) {
        array[i] = 0;
    }
}

int SafeArray::length() {
    return this-&gt;length_;
}

int SafeArray::boundsCheck(int y) {

}

int&amp; SafeArray::operator[] (int y) {
    return array[y];
}

SafeArray::~SafeArray() { 
    delete [] array;
}

SafeArray::SafeArray(SafeArray const &amp;other) { 
    int *temp = new int[rhs.size_];
    for (int i=0; i&lt;rhs.size_; i++)
        temp[i] = rhs.array[i];
    std::swap(temp, array);
    delete [] temp;
    return *this;
}
</code></pre>
