1

I am working on a school assignment that requires operator overloading. I am having difficulty with my subscript overload method. Have the return type set to int yet when I write a command like int myInt = myVector[i] I get a compiler error: C2440: '=' : cannot convert from 'MyVector' to 'int'

#include <stdexcept>
#include <string>
#include <iostream>
using namespace std;
const int DOUBLE = 2;

class MyVector
{
private:
    int size;
    int capacity;
    int* myArray;
public:
    MyVector();
    MyVector(int);
    int GetSize();
    int GetCapacity();
    ~MyVector();
    void clear();
    void Add_Value(int);
    //int operator[](int) const;
    int& operator[](int);
    friend ostream& operator<<(ostream&, MyVector*);
    bool operator==(MyVector&);
    bool operator!=(MyVector&);
};

#include "lab11.h"

MyVector::MyVector() : size(0), capacity(0){}
MyVector::MyVector(int i) : capacity(i), size(i)
{
    int* tempArray = new int[i];
    myArray = tempArray;
}
int MyVector::GetSize()
{
    return size;
}
int MyVector::GetCapacity()
{
    return capacity;
}
MyVector::~MyVector()
{
    delete [] myArray;
    delete myArray;
}
void MyVector::clear()
{
    delete [] myArray;
    delete myArray;
    myArray = new int[0];
}
void MyVector::Add_Value(int n)
{
    if (capacity == 0)
    {
        capacity = 1;
        size = 1;
        myArray = new int[1];
        myArray[0] = n;
        cout << myArray[0] << endl;
    }
    else
    {
        if (size == capacity)
        {
            int newCapacity = capacity * DOUBLE;
            int* tempArray = new int[newCapacity];
            for (int i = 0; i < size; i++)
            {
                tempArray[i] = myArray[i];  //copy contents to new array
                cout << tempArray[i] << endl;
            }
            capacity = newCapacity; 
            delete myArray;
            myArray = tempArray;
            myArray[size] = n;
            //delete []myArray;
            size++;

        }
        else
            myArray[size] = n;
    }
}
//int MyVector::operator[](int i)
//{
//  if (i < 0 || i > size -1)
//  {
//      string err = "Index out of bounds";
//      throw err;
//  }
//  else return myArray[i];
//}
int& MyVector::operator[](int i)
{
    if (i < 0 || i > size -1)
    {
        string err = "Index out of bounds";
        throw err;
    }
    else return myArray[i];
}
ostream& operator<<(ostream &out, MyVector* mv)
{
    int tmp;
    for (int i = 0; i < mv->GetSize(); i++)
    {
        tmp = mv[i];
        out << tmp << " ";
    }
    return out;
}
bool MyVector::operator==(MyVector &mv)
{
    if (size != mv.GetSize()) return false;
    else
    {
        for (int i = 0; i < size; i++)
        {
            if (myArray[i] != mv[i]) return false;
        }
    }
    return true;
}
bool MyVector::operator!=(MyVector &mv)
{
    if (size != mv.GetSize()) return true;
    else
    {
        for (int i = 0; i < size; i++)
        {
            if (myArray[i] != mv[i]) return true;
        }
    }
    return false;
}


void main()
{
    MyVector* mv = new MyVector(); //default constructor test
    mv->Add_Value(5);
    mv->Add_Value(6);
    mv->Add_Value(7);
    mv->Add_Value(8);
    cout << mv;
    system("PAUSE");
}
1
  • Please include the actual code that triggers the error message, and indicate it as such. Commented Dec 20, 2010 at 2:20

2 Answers 2

3

Try tmp = (*mv)[i]; in line 111.

You pass a pointer to MyVector in line 106, you need to dereference that pointer before applying operator[].

Sign up to request clarification or add additional context in comments.

Comments

1

Not sure why you commented out one op[], but you have a bug there.

//// You get an error here, because you forgot the "const" afterwards...
// int MyVector::operator[](int i)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.