在指向对象的指针向量中取消引用

发布于 2024-09-06 04:57:05 字数 962 浏览 13 评论 0原文

我正在尝试访问向量容器中保存的指针所指向的对象(称为向量),但我似乎无法访问它。

以下是重要的代码片段:

int main{
    Vector<double>* test = new Vector<double>(randvec<double>());

    test->save();

    cout << Element::vectors[0];
return 0;
}

其中 Vector 是模板类,randvec() 返回对向量的引用,save()

template <class T>
void Vector<T>::save()
{
    vectors.push_back(this);
}

并且向量是static std::vector;向量; 在 Element.h 中定义,Vectors 的基类。

我这一切都错了吗?我试图通过使用指向主类的指针向量将派生类的所有元素包含在基类的静态数据成员中。

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)'

为什么我不能取消引用该指针?

I'm trying to access the object (called a Vector) pointed to by a pointer held in a vector container, but I can't seem to get to it.

Here are the important code snippets:

int main{
    Vector<double>* test = new Vector<double>(randvec<double>());

    test->save();

    cout << Element::vectors[0];
return 0;
}

Where Vector is a template class, randvec<T>() returns a reference to a vector, save() is

template <class T>
void Vector<T>::save()
{
    vectors.push_back(this);
}

and vectors is static std::vector<Element*> vectors; defined in Element.h, the base class of Vectors.

Am I going about this all wrong? I'm trying to contain all the elements of a derived class in a static data member of the base class by using a vector of pointers to the main class.

My output from main() might tell you what's going on – I get the pointer 0x1001000a0. However, if I try to dereference that pointer, I get the following error:

error: no match for 'operator<<' in 'std::cout << * Element::vectors. 
std::vector<_Tp, _Alloc>::operator[] [with _Tp = Element*, _Alloc = std::allocator<Element*>](0ul)'

Why can't I dereference this pointer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

反差帅 2024-09-13 04:57:05

问题不在于解除引用。问题是“<<”没有为 Element::vectors 定义运算符

The problem is not with dereferencing. The problem is that "<<" operator is not defined for Element::vectors

﹉夏雨初晴づ 2024-09-13 04:57:05

您似乎缺少可用于输出 Elementoperator<< 重载。请注意,如果您只定义 Vector 的重载,它将不起作用,因为取消引用 Element::vectors[0] 会为您提供一个 类型的对象元素

(未经测试,抱歉)示例,说明如何允许派生类(例如 Vector)覆盖 Element 的流插入行为:

这是一个 Element的虚拟成员函数:

class Element
{
   // other stuff

   virtual void write_to_stream(std::ostream& stream) const = 0;
};

Element重载operator<<来调用该函数:

std::ostream& operator<<(std::ostream& stream, const Element& element)
{
    element.write_to_stream(stream);  // dynamic dispatch as we call through reference
    return stream;
}

然后重写派生类中的虚拟成员函数控制它们的书写方式:

template<class T>
class Vector : public Element
{
   // other stuff
   virtual void write_to_stream(std::ostream& stream) const
   {
      // whatever you like goes here
   }
};

It looks like you're missing an operator<< overload that can be used to output an Element. Note that it won't work if you just define the overload for Vector<T> because dereferencing Element::vectors[0] gives you an object of type Element.

Here's an (untested, sorry) example of how you can go about allowing derived classes (like Vector<T>) to override the stream-insertion behaviour of Element:

Add a virtual member function to Element:

class Element
{
   // other stuff

   virtual void write_to_stream(std::ostream& stream) const = 0;
};

Overload operator<< for Element to call this function:

std::ostream& operator<<(std::ostream& stream, const Element& element)
{
    element.write_to_stream(stream);  // dynamic dispatch as we call through reference
    return stream;
}

Then override the virtual member function in the derived classes to control how they should be written:

template<class T>
class Vector : public Element
{
   // other stuff
   virtual void write_to_stream(std::ostream& stream) const
   {
      // whatever you like goes here
   }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文