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

C

claude uq

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;
}
*/
 
G

Gianni Mariani

claude said:
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 ?
 
J

Jeff Schwab

Except that it makes your program work.
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;
}



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.
 
T

tom_usenet

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top