我正在尝试访问由向量容器中保存的指针指向的对象(称为向量),但我似乎无法访问它。
以下是重要的代码片段:
int main{
Vector<double>* test = new Vector<double>(randvec<double>());
test->save();
cout << Element::vectors[0];
return 0;
}
Vector
模板类在哪里,randvec<T>()
返回对向量的引用,save()
是
template <class T>
void Vector<T>::save()
{
vectors.push_back(this);
}
向量static std::vector<Element*> vectors;
在 Vectors 的基类 Element.h 中定义。
我对这一切都错了吗?我试图通过使用指向主类的指针向量将派生类的所有元素包含在基类的静态数据成员中。
我的 main() 输出可能会告诉你发生了什么——我得到了指针0x1001000a0
。但是,如果我尝试取消引用该指针,我会收到以下错误:
error: no match for 'operator<<' in 'std::cout << * Element::vectors.
std::vector<_Tp, _Alloc>::operator[] [with _Tp = Element*, _Alloc = std::allocator<Element*>](0ul)'
为什么我不能取消引用这个指针?