By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

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

P: n/a
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;
}
*/
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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 ?
Jul 22 '05 #2

P: n/a
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.

Jul 22 '05 #3

P: n/a
On Thu, 18 Dec 2003 15:41:31 +1000, "claude uq" <c.********@uq.edu.au>
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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.