c++在尝试重载下标运算符时出现转换错误

标签 c++ operator-overloading

我正在做一项需要运算符重载的学校作业。我的下标重载方法有困难。将返回类型设置为 int 但当我编写类似 int myInt = myVector[i] 的命令时,我收到编译器错误: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");
}

最佳答案

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

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

关于c++在尝试重载下标运算符时出现转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4486403/

相关文章:

c++ - 与 C++ 中的模板类混淆

python - 通过解释器查看所有 python 运算符的列表

C++:关于重载 operator new 的一般想法

c++ - 错误 : invalid conversion from ‘int’ to ‘const char*’

c++ - 为什么删除的复制构造函数不允许使用其他具有多态类型的构造函数?

c++ - 在CMake中使用find_package时,是否会显式包含 header ?

c++ - 错误 'operator*' 不匹配

c++ - 在 C++ 模板函数中根据所需类型进行转换的标准方法是什么

c++ - 为 float 全局重载 == 和 !=

c++ - 重载新运算符和继承