moiself
moiself

Reputation: 55

Templated class fails taking other templated class

I have a templated class that takes a reference to that template as single argument in the constrcutor. This works fine untill the template parameter becomes a class that is also templated. The compiler (VS 2008) gives me a couple of errors but I can't find out how to solve this...

EDIT this is from the actual source :

PerlinNoise<> per;
RawPainter< PerlinNoise<> > pat(per);
TextureGenerator<RawPainter<PerlinNoise<> > genn(pat);

where PerlinNoise has default template parameters and both RawPainter and TextureGenerator take a reference (from type T) in the constructor

So, how can I make this work ? It is probably a simple extra typename somewhere but I can't seem to figure it out.

thx

Upvotes: 0

Views: 166

Answers (2)

sehe
sehe

Reputation: 394054

Pass the correct reference into the constructor:

A<A<int> > myAA;
B< A< A<int> > > myB3(myAA); 

Upvotes: 1

David Schwartz
David Schwartz

Reputation: 182893

 int pod = 5;
 B<int> myB(pod); //OK

This works. B<int> requires an int& to contstruct. Since pod is an int, it can trivially be converted into the correct type.

A<int> myA;
B< A< A<int> > > myB2(myA); //COMPILE ERROR

Well, this can't work. The constructor for B< A< A< int > > > requires an A< A< int > > and you pass it an A< int >. There's no conversion available, so there's no way to get the right type to pass to the constructor.

Your compiler should have explained this to you. Mine did:

error: no matching function for call to B<A<A<int> > >::B(A<int>&)
note: candidates are:
note: B<T>::B(T&) [with T = A<A<int> >]
note:   no known conversion for argument 1 from A<int> to A<A<int> >&
note: B<A<A<int> > >::B(const B<A<A<int> > >&)
note:   no known conversion for argument 1 from A<int> to const B<A<A<int> > >&

This is the give away: no known conversion for argument 1 from A<int> to A<A<int> >&. The last line tells you the compiler also tried to use an implicit copy constructor, but that failed too.

Upvotes: 2

Related Questions