Topic: std::vector question


Author: Tomer <barletz@gmail.com>
Date: Mon, 26 Nov 2007 11:51:21 CST
Raw View
Hi,
I have the following scenario:

class A {};

class B : public A {};

void TestingA(std::vector<A*> Tesing) {}

main()
{
  std::vector<B*> Bs;
  TestingA(Bs);
}

On VC++ 2008 I get the following error:
error C2664: 'TestingA' : cannot convert parameter 1 from
'std::vector<_Ty>' to 'std::vector<_Ty>'
        with
        [
            _Ty=B *
        ]
        and
        [
            _Ty=A *
        ]
        No user-defined-conversion operator available that can perform
this conversion, or the operator cannot be called

Can you please explain?

Thanks,
Tomer

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: francis.glassborow@btinternet.com (Francis Glassborow)
Date: Mon, 26 Nov 2007 19:36:48 GMT
Raw View
Tomer wrote:
> Hi,
> I have the following scenario:
>
> class A {};
>
> class B : public A {};
>
> void TestingA(std::vector<A*> Tesing) {}
>
> main()
> {
>   std::vector<B*> Bs;
>   TestingA(Bs);
> }
>
> On VC++ 2008 I get the following error:
> error C2664: 'TestingA' : cannot convert parameter 1 from
> 'std::vector<_Ty>' to 'std::vector<_Ty>'
>         with
>         [
>             _Ty=B *
>         ]
>         and
>         [
>             _Ty=A *
>         ]
>         No user-defined-conversion operator available that can perform
> this conversion, or the operator cannot be called
>
> Can you please explain?
>

There is no relationship between a container of A* and a container of B*
even though there is a relationship between A and B. IIRC this is not
limited to the pointer case, there are no builtin conversions between
template specialisations for different types regardless of the types
regardless of any conversions (user defined or implicit) between the types.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]