returning pointer to template objects in nested template classes

J

Javier Montoya

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

// bar.cpp
#include <bar.h>
....
....
#include "bar-impl.inc"

// bar-impl.inc
template class Bar<int>;
template class Bar<float>;

// 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

// foo.cpp
#include <foo.h>
.....
template < class T >
template <class BarType>
BarType * Foo<T>::doSomething(int idx){
int num = this->getSomething(idx);
BarType * pBar = new BarType(num);
return pBar;
}
.......
#include "foo-impl.inc"

// foo-impl.inc
template class Foo<int>;
template class Foo<float>;
template Bar<float> * Foo<int>::doSomething(int);
template Bar<float> * Foo<float>::doSomething(int);

// 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);

return (0);
}
 
M

Michael Tsang

Javier said:
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.
 
J

Javier Montoya

Javier said:
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:

template < class T >
class Foo{
public:
.....
int getSomething(int);
....
template <class BarType>
void doSomething(int, BarType *);
....
};

Best
 
A

Alen Wesker

Try to put this line into your Foo.cpp:
#include "bar-impl.inc"

You have seperated the implementation and the declaration of the Bar
Template, haven't you?

In this situation, the declaration h file could not substitute the
real implementation, and the compiler would refuse to work until it
sees the real implementation when it try to expland the Bar code. It
is discused in the book <C++ Template>.

I am not quit sure about the situation, maybe you can try what I said
at first.
 
A

Ali_Raz

Javier said:
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)
(impl skipped)
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.

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top