On Nov 18, 10:17*am, Javier Montoya <jmonto...@gmail.com> wrote:
> On Nov 18, 4:02*pm, Michael Tsang <mikl...@gmail.com> wrote:
>
>
>
> > Javier Montoya wrote:
> > > Hi everybody!
>
> > > I've two template classes, say Bar<S> and Foo<T>. Inside of the Foo<T>
> > > I've a method that returns Bar<S>*. After compiling the code, I get an
> > > error of:
> > > error: no matching function for call to
> > > ‘CBar<float>::doSomething(int&)’
> > > make: *** [main] Error 1
>
> > > Does somebody has an idea about how could I solve the problem? Below
> > > the structure of my code:
>
> > > // bar.h
> > > #ifndef BAR_H
> > > #define BAR_H
> > > template < class S >
> > > class Bar{
> > > * ....
> > > };
> > > #endif
>
> > (impl skipped)
> > > // foo.h
> > > #ifndef FOO_H
> > > #define FOO_H
> > > #include <bar.h>
> > > template < class T >
> > > class Foo{
> > > public:
> > > ....
> > > int getSomething(int);
> > > ...
> > > template <class BarType>
> > > BarType * doSomething(int);
> > > ...
> > > };
> > > #endif
>
> > (impl skipped)
> > > // main.cpp
> > > #include <bar.h>
> > > #include <foo.h>
>
> > > int main(){
> > > Bar<float> * pBar = NULL;
> > > int num=4;
> > > Foo<float> * pFoo = new Foo<float>();
> > > pBar = pFoo->doSomething(num);
>
> > There is a problem in your usage of Foo<float>::doSomething.
> > Foo<float>::doSomething is a function template but there aren't any
> > parameters which enable the compiler to guess what BarType is (remember, the
> > return type is *not* used in guessing the template parameters). Therefore,
> > the compiler cannot select a suitable BarType and generate an error.
>
> > > return (0);
> > > }
>
> Thanks Michael! so the only solution would be to pass "BarType *" as a
> parameter, right? or is there any other possible solution:
No, that's not a good solution. You can use explicit instantiation,
e.g., pBar->doSomething<Bar<float> >(num); Note that you may need to
use the template keyword if your calling code resides in a template,
that is, pBar->template doSomething<Bar<float> >(num).
>
> template < class T >
> class Foo{
> public:
> ....
> int getSomething(int);
> ...
> template <class BarType>
> void doSomething(int, BarType *);
> ...
>
> };
>
> Best
|