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

时间:2017-10-22 12:15:57

标签: c++ vector

我是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

1 个答案:

答案 0 :(得分:0)

您需要在operator[]课程中定义Vector方法。由于您没有提供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是要获取的元素的偏移量。