Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > returning pointer to template objects in nested template classes

Reply
Thread Tools

returning pointer to template objects in nested template classes

 
 
Javier Montoya
Guest
Posts: n/a
 
      11-18-2010
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);
}
 
Reply With Quote
 
 
 
 
Michael Tsang
Guest
Posts: n/a
 
      11-18-2010
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);
> }


 
Reply With Quote
 
 
 
 
Javier Montoya
Guest
Posts: n/a
 
      11-18-2010
On Nov 18, 4:02*pm, Michael Tsang <(E-Mail Removed)> 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:

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

Best
 
Reply With Quote
 
Alen Wesker
Guest
Posts: n/a
 
      11-19-2010
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.

 
Reply With Quote
 
Ali_Raz
Guest
Posts: n/a
 
      11-20-2010
On Nov 18, 10:17*am, Javier Montoya <(E-Mail Removed)> wrote:
> On Nov 18, 4:02*pm, Michael Tsang <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with a function template returning a private nested class ofa class template ymost@hotmail.com C++ 2 12-28-2008 10:43 AM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
Nested Vector Nester Classes are Nested in my Brain Chad E. Dollins C++ 3 11-08-2005 04:46 AM
Nested Classes vs Top-Level Classes kelvSYC Java 2 08-18-2004 07:09 AM
What is the difference between nested classes and inner classes ? Razvan Java 5 07-27-2004 07:59 PM



Advertisments