C++ 无法检索自定义矢量中的对象
C++ Unable to retrieve Objects in Custom 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
您需要
在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
是要获取的元素的偏移量。
相关文章:
- 自定义对象的dlib序列化在gcc中失败
- 如何将带有自定义对象的容器从C++传递到QML
- 将 exprtk 与自定义类的对象一起使用
- 将 RTOS 队列对象封装在仅具有静态分配的 IQueue 自定义接口中
- 如何实现自定义匹配器以检查 Catch2 中的对象相等性
- 如何使用Q_PROPERTY公开自定义对象列表
- Qt5 远程对象 + 自定义类型,但不在 POD 中
- 从自定义类获取对象向量中的 max 元素
- 如何将来自 Boost.Python 的map_indexing_suite与自定义而不是标准对象一起使用?
- 如何在自定义对象的<<运算符中添加自定义前缀
- 如何使用 STL 排序对具有模板专用化的自定义类对象进行排序?
- 如何从 cpp 中的函数返回自定义类对象?
- 如何为自定义模板对象创建专门的函数模板
- 带有自定义deleter的std::unique_ptr对象的大小(一个由ref捕获的lambda)
- 基于3个对象的自定义排序
- 如何键入用于const对象的自定义io操纵器
- 指向临时对象的自定义迭代器(延迟加载)
- 将自定义对象作为参数从目标 C 传递到 C++
- 从 Arduino 中的自定义C++库构造对象时出错
- 从链表访问对象(自定义实现)