C++ 无法检索自定义矢量中的对象

C++ Unable to retrieve Objects in Custom Vector

本文关键字:对象 自定义 检索 C++      更新时间:2023-10-16

我是C++新手,当我尝试在自定义向量中检索对象时遇到此错误消息。任何帮助将不胜感激!谢谢!

错误消息:错误:运算符 []' 不匹配(操作数类型为"Vector"和"int")

背景:我正在尝试将我的输入与 Vector 中的对象进行比较。

for (int i = 0; i < size; i++) {
 // Error: no match for operator[]' (operand types are 'Vector<Windlog>' and 'int')
 Windlog wl = MyVector[i];
    //int month = wl.GetDate().GetMonth();
    //int year = wl.GetDate().GetYear();
if(inputMonth == month && inputYear == year) {
// Then do something;
}
} // End of For-Loop

我的向量类

#ifndef VECTOR_H
#define VECTOR_H
#include "Windlog.h"
#include "Date.h"
#include "Time.h"
#include <iostream>
#include <string>
using namespace std;
template <class DataType>
class Vector {
private:
    int size;
    int capacity;
    DataType *myArray;
    DataType *start;
    DataType *finish;
public:
    Vector();
    void clear();
    void create(int x);
    void resize(int newsize);
    void pushback(DataType data);
    void print();
    int GetArraySize();
};
template <class DataType>
Vector<DataType>::Vector() {
    clear();
    create(2);
}

template <class DataType>
void Vector<DataType>::clear() {
    myArray = NULL;
    start = NULL;
    finish = NULL;
    size = 0;
    capacity = 0;
}
template <class DataType>
void Vector<DataType>::create (int x) {
    myArray = new DataType[x];
    start = myArray;
    finish = myArray;
    capacity = x;
    size = 0;
}

template <class DataType>
void Vector<DataType>::resize(int newsize) {
    DataType *tempArray = new DataType[newsize];
    DataType *tempStart = tempArray;
    DataType *oldArrayPointer = myArray;
    while (oldArrayPointer != myArray+size) {
        *tempStart = *oldArrayPointer;
        tempStart++;
        oldArrayPointer++;
    }
    start = tempArray;
    finish = tempArray+size;
    capacity = newsize;
    delete[] myArray;
    myArray = tempArray;
}
template <class DataType>
void Vector<DataType>::pushback(DataType data) {
    if (size == (capacity - 1)) {
    resize(capacity * 2);
    }
    *finish = data;
    finish++;
    size++;
}
template <class DataType>
int Vector<DataType>::GetArraySize() {
    return size;
}
template <class DataType>
void Vector<DataType>::print() {
    /*
    cout << "capacity: " << capacity <<endl;
    cout << "size: " << size <<endl;
    cout << "myArray: " << *myArray <<endl;
    cout << "start: " << *start <<endl;
    cout << "finish: " << *(finish-1) <<endl;

    cout << "Data:";
    DataType *target = myArray;
    while (target != finish) {
    cout << *target << endl;
    target++;
    }
    */
    cout << "size: " << size <<endl;
    cout << endl;
}

#endif // INTVEC_H_INCLUDED
您需要

Vector类中定义一个operator[]方法。由于您不提供Vector的实现,因此我做了一些假设,并提供了一个如何实现它的示例。

template <typename T>
class Vector {
private:
    T* data_;
    // std::size_t length_;
    // std::size_t capacity_;
public:
    // constructors, destructor and observer methods...
    T& operator[](std::size_t pos) & {
        return data_[pos];
    }
    const T& operator[](std::size_t pos) const& {
        return data_[pos];
    }
};

在我的示例中,data_是一个指针,表示连续内存块的开头,pos是要获取的元素的偏移量。