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:53:17 GMT  
 error C2440 when try to compile template class with overloaded operator

Smart pointers are NOT pointers, remember? Just because A and B are related doesn't make
Ptr<A> and Ptr<B> related as well.



Sat, 14 Jun 2003 06:19:19 GMT  
 error C2440 when try to compile template class with overloaded operator
You are right, But if I want to implement this relationship, I can do it by
implement overloading of type casting operator. See B.Stroustrup "The C++
pr.lang." p.13.6.3.1).
Therewith it does work at Borland.


Quote:

> Smart pointers are NOT pointers, remember? Just because A and B are

related doesn't make
Quote:
> Ptr<A> and Ptr<B> related as well.



Sat, 14 Jun 2003 15:30:58 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