簡體   English   中英

嘗試重載下標運算符時c ++收到轉換錯誤

[英]c++ getting conversion error when trying to overload subscript operator

我正在進行一項需要操作員超載的學校作業。 我的下標重載方法有困難。 當我寫一個像int myInt = myVector[i]這樣的命令時,返回類型設置為int我得到一個編譯器錯誤: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");
}

試試tmp = (*mv)[i]; 在111行。

您在第106行傳遞指向MyVector的指針,您需要在應用operator []之前取消引用該指針。

不確定為什么你注釋掉了一個op [],但你有一個bug。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM