tags:

views:

91

answers:

5

Hi,

First of all I'd like to apologize if there's another question like this one and I didn't find it. I've been trying but since the problem is quite specific I couldn't find one.

Now, the problem. I have a Base class and a Derived class (say BNode and DNode ), and I have a std::vector of BNode*. I have also a function which receives a reference to a vector of these pointers. I'm having trouble when trying to pass a std::vector of pointers to derived objects as a parameter to this function:

class BNode
{
};

class DNode : public BNode
{
};

class Other
{
function(std::vector<BNode*>& inputVector) { } 
}

When trying to pass a vector of pointers to the derived class, the error I'm receiving from VS is:

1> error C2664: 'Other::function' : cannot convert parameter 1 'std::vector<T>' to 'std::vector<T> &'
1>        with
1>        [
1>            T=DNode *
1>        ]
1>        and
1>        [
1>            T=BNode *
1>        ]

Thanks beforehand.

+3  A: 

This is because vector< Bnode* > and vector< Dnode* > do not have any parent-child relation. They are different objects.

I guess you have created a vector like std::vector< DNode* > dnodevec; Instead you can create std::vector< BNode* > dnodevec; Being the vector of base class pointers you can safely insert any DNode type pointer in the vector.

Manoj R
Thanks a lot! It worked perfectly!
gbombay
+1  A: 

Your problem is that even if DNode derives from BNode, std::vector<DNode*> doesn't derive from std::vector<BNode*>.

Therefore, the compiler does not know how to convert from one type to the other.

You might however do:

std::vector<BNode*> bnode_list;
std::vector<DNode*> dnode_list(bnode_list.begin(), bnode_list.end());
ereOn
A: 

C++ doesn't support covariance in templates. An std::vector<DNode*> cannot be assigned to an std::vector<BNode*> even if DNode derives from BNode.

For your code to work, you should only declare vectors of pointers to BNode and populate them with pointers to DNode as needed.

Frédéric Hamidi
+1  A: 

I suspect you are probably calling Other::function something like this:

std::vector<DNode *> v;
// put stuff into v
Other o;
o.function(v);

Which won't compile, as std::vector<DNode *> is a distinct type to std::vector<BNode *>.

What may be suitable is:

std::vector<BNode *> v;
// You can put DNode * items into v
Other o;
o.function(v);
spong
A: 

Unfortunately, what you want is not possible in C++, because vector<BNode*> and vector<DNode*> are considered to be unrelated types (even though there is a clear relation between BNode and DNode).

Consider the following code:

class BNode
{
};

class DNode : public BNode
{
};

class XNode : public BNode
{
};

void function(std::vector<BNode*>& inputVector) 
{
  inputVector.push_back(new XNode); // This will work, as XNode derived from BNode
} 

int main()
{
  std::vector<DNode*> vec;

  function(vec); // Suppose this would work
  // Then now, vec would contain a pointer to the unrelated class XNode!!
}
Bart van Ingen Schenau
That is true in this case, would not apply if the vector were immutable. C++ is somewhat flawed in this regard, although if the purpose is to call some virtual method on every item, it is possible to do with for_each.
CashCow