Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance issues with template classes

I'm having issues with a very strange error in some code I wrote. The basic idea behind the code can be trivialised in the following example:


template <class f, class g> class Ptr;

template <class a, class b, class c = Ptr<a,b> > class Base { public: Base(){}; };

template <class d, class e> class Derived : public Base <d,e> { public: Derived(){}; };

template <class f, class g> class Ptr { public: Ptr(){}; Ptr(Base<f,g,Ptr<f,g> >* a){}; };

typedef Derived<double,double> DDerived;

int main() { Base<int,int> b = Base<int,int>(); Derived<double,double> d = Derived<double,double>(); DDerived dd = DDerived(); Ptr<double,double> p(&dd); return 1; }

The basic idea is that pointers are replaced by the Ptr class (This will eventually be used in an MPI setting so standard pointers are effectively useless). The pointers are designed to 'point' at the base class, and so can point at any inherited class (as demonstrated in the example).

Can anyone think of any reason this might not work in a non-trivial case (But a case where the object architecture remains identical).

The error that is occurring in the main case is as follows:


void function()
{
  vector nVector(1);  // cut down for simplicity
  nVector[0].SetId(1); // To ensure the node is instantiated correctly
  Ptr temp(&nVector[1]);
};

This code produces the (slightly extended version of the) error when compiled with MPICXX:
no matching function for call to Ptr&lt;double, double>::Ptr(Derived&lt;double, double>*)
candidates are . . . (Some removed for simplicity's sake)
Ptr&lt;f, g>::Ptr(Base&lt;f, g, Ptr&lt;f, g> >*) [with f = double, g = double]

Cheers, Ed

EDITED (Detailing the error a little better, added info on the compiler)

like image 442
Ed James Avatar asked Aug 08 '26 04:08

Ed James


1 Answers

Unfortunately, I was being slightly stupid and had forgotten to put my Ptr class in the same namespace as the Base and Derived classes.

That, I guess, would be why it wasn't working ! =]

like image 124
Ed James Avatar answered Aug 10 '26 17:08

Ed James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!