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