473,467 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
3 2930
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
0
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an...
4
by: Mark P | last post by:
I'm trying to define a templatized version of operator<< to print out information about a template class. The code below won't compile. (error: no match for std::ostream& << Outer<int>::Inner&) ...
6
by: Howard | last post by:
Hi, I have a function in three unrelated but similar classes. The code in the member functions is identical for all three classes. What I want is to make a template which defines the function,...
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
11
by: rogo | last post by:
Ok, when I began to implement it, I thought it should be straightforward and easy. I'm not sure whats wrong with the following code: class A { public: A() {} int get() {
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.