error C2440 when try to compile template class with overloaded operator 
Author Message
 error C2440 when try to compile template class with overloaded operator

Hi,
I receive "error C2440", when I try to compile the following code:

#include "stdafx.h"
#include <iostream.h>
#include <string.h>

template<class T> class Ptr{
public:
  T* p;
  Ptr(T* ss) : p(ss){};
  template<class T2> operator Ptr<T2>(){
   return Ptr<T2> (p);
  }

Quote:
};

class A{
public:
  virtual void print(){cout<<"class A"<<'\n';}

Quote:
};

class B : public A{
public:
 void print(){cout<<"class B:"<<'\n';}

Quote:
};

void f(Ptr<B> bb)
{
 Ptr<A> a = bb;  /* -> this line is not compiled: error C2440 */
 a.p->print();

Quote:
}

int main( int argc, char* argv[] )
{
 Ptr<B> b(new B);
 f(b);
 cout << endl;
 return 0;

Quote:
}

At the same time, in C++ Builder 4 it works.
What should be done in order that the compilation will work in VC?
Thanks,
Efim


Fri, 13 Jun 2003 17:52:17 GMT  
 error C2440 when try to compile template class with overloaded operator

Greets,

    Apparently this is a known issue and there are some workarounds.  You
may wish to take a peek at the following knowledge base article for an
explanation of the problem and the solutions:

ID: Q149327 : "FIX: C2440 Expanding Template with a Pointer to a Nested
Class"

Regards,

Joe


Quote:
> Hi,
> I receive "error C2440", when I try to compile the following code:

> #include "stdafx.h"
> #include <iostream.h>
> #include <string.h>

> template<class T> class Ptr{
> public:
>   T* p;
>   Ptr(T* ss) : p(ss){};
>   template<class T2> operator Ptr<T2>(){
>    return Ptr<T2> (p);
>   }
> };

> class A{
> public:
>   virtual void print(){cout<<"class A"<<'\n';}
> };

> class B : public A{
> public:
>  void print(){cout<<"class B:"<<'\n';}
> };

> void f(Ptr<B> bb)
> {
>  Ptr<A> a = bb;  /* -> this line is not compiled: error C2440 */
>  a.p->print();
> }

> int main( int argc, char* argv[] )
> {
>  Ptr<B> b(new B);
>  f(b);
>  cout << endl;
>  return 0;
> }

> At the same time, in C++ Builder 4 it works.
> What should be done in order that the compilation will work in VC?
> Thanks,
> Efim



Mon, 16 Jun 2003 21:35:33 GMT  
 error C2440 when try to compile template class with overloaded operator
I saw it.
Unfortunately, it is not my case.
The problem(and maybe bug) is that VC compiler attempts to use copy
constructor instead and does not use casting operator.

Efim

Quote:
> Greets,

>     Apparently this is a known issue and there are some workarounds.  You
> may wish to take a peek at the following knowledge base article for an
> explanation of the problem and the solutions:

> ID: Q149327 : "FIX: C2440 Expanding Template with a Pointer to a Nested
> Class"

> Regards,

> Joe



> > Hi,
> > I receive "error C2440", when I try to compile the following code:

> > #include "stdafx.h"
> > #include <iostream.h>
> > #include <string.h>

> > template<class T> class Ptr{
> > public:
> >   T* p;
> >   Ptr(T* ss) : p(ss){};
> >   template<class T2> operator Ptr<T2>(){
> >    return Ptr<T2> (p);
> >   }
> > };

> > class A{
> > public:
> >   virtual void print(){cout<<"class A"<<'\n';}
> > };

> > class B : public A{
> > public:
> >  void print(){cout<<"class B:"<<'\n';}
> > };

> > void f(Ptr<B> bb)
> > {
> >  Ptr<A> a = bb;  /* -> this line is not compiled: error C2440 */
> >  a.p->print();
> > }

> > int main( int argc, char* argv[] )
> > {
> >  Ptr<B> b(new B);
> >  f(b);
> >  cout << endl;
> >  return 0;
> > }

> > At the same time, in C++ Builder 4 it works.
> > What should be done in order that the compilation will work in VC?
> > Thanks,
> > Efim



Mon, 16 Jun 2003 23:01:39 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. error C2440 when try to compile template class with overloaded operator

2. error c2440 returned trying to compile overloaded [] operator

3. class templates and overloaded operators

4. operator overloading in template class

5. Operator overloading in a template class ?

6. Error C2440 when trying to call DialogBox

7. compiling error for template class definition

8. update overloading [][] ..working but not with a template operator ..arrrrrrrrrgghghhh

9. Template functions & overloaded operators

10. Help! Double-Linked List, Templates, and Operator Overloading

11. Resolving overload Template operator<<() with VS 2003

12. overloading operator<< in template

 

 
Powered by phpBB® Forum Software