Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template functions calls within non template classes. How to do it?

Reply
Thread Tools

template functions calls within non template classes. How to do it?

 
 
claude uq
Guest
Posts: n/a
 
      12-18-2003
Hello,

Am trying to use template functions within some class to convert int, float,
doubles, etc into strings.

Below, three ways to do it via use of "to_string(const T & Value)" . The
only one that works is the first which requires "DataManager" to be a
template class. Since I don't want "DataManager" to be a template class (no
advantages), I am trying various ways and none is working. Did quite a bit
of search via Google on this but have remained blank

Anyone enlightening me here would be very appreciated.

Ciao

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

/////////////////////////////
// Following works
/////////////////////////////
template <class T>
class DataManager {
public:
inline string to_string(const T & Value);
};

template<class T>
inline string DataManager<T>::to_string(const T & Value) {
stringstream streamOut;
streamOut << Value;
return streamOut.str( );
}
int main () {
DataManager<int> dataManagerInt;
DataManager<double> dataManagerDbl;
string strInt = dataManagerInt.to_string(200346);
string strDbl = dataManagerDbl.to_string(200.346);
cout << strInt + " and " + strDbl << endl;
system("pause");
return 0;
}


////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////
// Following clearly can't work since T not specified in any way in class
definition
////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////
class DataManager {
public:
inline string to_string(const T & Value);
};

template<class T>
inline string DataManager<T>::to_string(const T & Value) {
stringstream streamOut;
streamOut << Value;
return streamOut.str( );
}
int main () {
DataManager dataManager;
string str = dataManager.to_string<int>(200346);
cout << str << endl;
system("pause");
return 0;
}


////////////////////////////////////////////////////////////////////////////
/////////
// Following does not work. I can't understand why !!
////////////////////////////////////////////////////////////////////////////
/////////
class DataManager {
public:
template<class T>
inline string to_string(const T & Value) {
stringstream streamOut;
streamOut << Value;
return streamOut.str( );
}
};

int main () {
DataManager dataManager;
string str = dataManager.to_string<int>(200346);
cout << str << endl;
system("pause");
return 0;
}
*/


 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      12-18-2003
claude uq wrote:
> Hello,
>
> Am trying to use template functions within some class to convert int, float,
> doubles, etc into strings.
>
> Below, three ways to do it via use of "to_string(const T & Value)" . The
> only one that works is the first which requires "DataManager" to be a
> template class. Since I don't want "DataManager" to be a template class (no
> advantages), I am trying various ways and none is working. Did quite a bit
> of search via Google on this but have remained blank


This is one example.

#include <sstream>
#include <iostream>
#include <string>
using namespace std;

class DataManager {
public:

template <class T>
inline string to_string(const T & Value)
{
stringstream streamOut;
streamOut << Value;
return streamOut.str( );
}
};

//
// no class version
//
template <class T>
inline string to_string(const T & Value)
{
stringstream streamOut;
streamOut << Value;
return streamOut.str( );
}


int main () {

DataManager dataManager;

string strInt = dataManager.to_string(200346);

string strDbl = dataManager.to_string(200.346);

cout << strInt + " and " + strDbl << endl;

cout << "to_string( 88.99F ) = " << to_string( 88.99F ) << endl;

return 0;
}


>
> Anyone enlightening me here would be very appreciated.
>
>


Why do you want a "DataManager" class ?


 
Reply With Quote
 
 
 
 
Jeff Schwab
Guest
Posts: n/a
 
      12-18-2003
Gianni Mariani wrote:
> claude uq wrote:
>
>> Hello,
>>
>> Am trying to use template functions within some class to convert int,
>> float,
>> doubles, etc into strings.
>>
>> Below, three ways to do it via use of "to_string(const T & Value)" . The
>> only one that works is the first which requires "DataManager" to be a
>> template class. Since I don't want "DataManager" to be a template
>> class (no advantages),


Except that it makes your program work.

>> I am trying various ways and none is working. Did quite
>> a bit of search via Google on this but have remained blank

>
>
> This is one example.
>
> #include <sstream>
> #include <iostream>
> #include <string>
> using namespace std;
>
> class DataManager {
> public:
>
> template <class T>
> inline string to_string(const T & Value)
> {
> stringstream streamOut;
> streamOut << Value;
> return streamOut.str( );
> }
> };
>
> //
> // no class version
> //
> template <class T>
> inline string to_string(const T & Value)
> {
> stringstream streamOut;
> streamOut << Value;
> return streamOut.str( );
> }
>
>
> int main () {
>
> DataManager dataManager;
>
> string strInt = dataManager.to_string(200346);
>
> string strDbl = dataManager.to_string(200.346);
>
> cout << strInt + " and " + strDbl << endl;
>
> cout << "to_string( 88.99F ) = " << to_string( 88.99F ) << endl;
>
> return 0;
> }
>
>
>>
>> Anyone enlightening me here would be very appreciated.
>>
>>

>
> Why do you want a "DataManager" class ?


Good question. This would be a great use for an ordinary namespace,
e.g. Data_Management. Perhaps the OP is a Java coder.

 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      12-18-2003
On Thu, 18 Dec 2003 15:41:31 +1000, "claude uq" <(E-Mail Removed)>
wrote:

>Hello,
>
>Am trying to use template functions within some class to convert int, float,
>doubles, etc into strings.
>
>Below, three ways to do it via use of "to_string(const T & Value)" . The
>only one that works is the first which requires "DataManager" to be a
>template class. Since I don't want "DataManager" to be a template class (no
>advantages), I am trying various ways and none is working. Did quite a bit
>of search via Google on this but have remained blank
>
>Anyone enlightening me here would be very appreciated.


You appear to have a faulty compiler. The third example should work
fine.

>#include <iostream>
>#include <string>
>#include <sstream>
>using namespace std;


>////////////////////////////////////////////////////////////////////////////
>/////////
>// Following does not work. I can't understand why !!
>////////////////////////////////////////////////////////////////////////////
>/////////
>class DataManager {
> public:
> template<class T>
> inline string to_string(const T & Value) {


inline is redundant inside a class definition.

> stringstream streamOut;
> streamOut << Value;
> return streamOut.str( );
> }
>};
>
>int main () {
> DataManager dataManager;
> string str = dataManager.to_string<int>(200346);
> cout << str << endl;
> system("pause");
> return 0;
>}


Apart from not #including <cstdlib>, that looks fine (and compiles on
VC 7.1). If you're using VC6, it is notoriously bad with member
templates.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
Using variadic functions within variadic functions pinkfloydhomer@gmail.com C Programming 2 02-27-2006 05:47 AM
Inability to explicitly specialize a template class within another non-specialized template class jn@ngedit.com C++ 1 02-17-2006 07:17 PM



Advertisments