尝试重载下标运算符时c ++收到转换错误

时间:2010-12-20 02:17:53

标签: c++ operator-overloading

我正在完成一项需要操作员重载的学校作业。我的下标重载方法有困难。将返回类型设置为int但是当我编写像int myInt = myVector[i]这样的命令时,我收到编译器错误:C2440:'=':无法从'MyVector'转换为'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");
}

2 个答案:

答案 0 :(得分:3)

在第111行尝试tmp = (*mv)[i];

你在第106行传递一个指向MyVector的指针,你需要在应用operator []之前取消引用该指针。

答案 1 :(得分:1)

不确定为什么你注释掉了一个op [],但你有一个错误。

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