views:

109

answers:

2

I want to keep the class simple and not defined a constructor so i can do Pt data = {0, 5}; so i figured the best way convert Pt_t from a short to long or vice versa is to do something like this.

template <class T>
struct Pt_t
{
    T x, y;
    template <class T2> operator Pt_t<T2>() { Pt_t pt = {x, y}; return pt; }
};

The compiler doesnt like this and calls operator Pt_t on return pt; thus getting a stack overflow. How do i prevent this? the only solution i can think of is having Pt_t use constructors removing Pt_t pt = {1, 2}; which i prefer to keep if i can.

+1  A: 

I'm unfamiliar with C++, but are you declaring the right type in your method there?

Shouldn't it be Pt_t<T2> instead of just Pt_t ?

Lasse V. Karlsen
+4  A: 

I'm pretty sure the unqualified Pt_t in your functions body is Pt_t<T>, but don't you want it to be Pt_t<T2>? You'll need to explicitly qualify it.

eduffy
Thank you, that was my error. For those who want to see code, the correct function is template <class T2> operator Pt_t<T2>() { Pt_t<T2> pt = {x, y}; return pt; }
acidzombie24