Nick Sweet
Nick Sweet

Reputation: 2139

dereferencing in a vector of pointers to objects

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?

Upvotes: 2

Views: 2953

Answers (2)

Mike Dinsdale
Mike Dinsdale

Reputation: 1511

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
   }
};

Upvotes: 1

Pardeep
Pardeep

Reputation: 949

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

Upvotes: 2

Related Questions