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

Discussion in 'C++' started by claude uq, Dec 18, 2003.

  1. claude uq

    claude uq Guest

    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;
    }
    */
     
    claude uq, Dec 18, 2003
    #1
    1. Advertising

  2. Re: template functions calls within non template classes. How todo it?

    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 ?
     
    Gianni Mariani, Dec 18, 2003
    #2
    1. Advertising

  3. claude uq

    Jeff Schwab Guest

    Re: template functions calls within non template classes. How todo it?

    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.
     
    Jeff Schwab, Dec 18, 2003
    #3
  4. claude uq

    tom_usenet Guest

    On Thu, 18 Dec 2003 15:41:31 +1000, "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:-(
    >
    >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
     
    tom_usenet, Dec 18, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Honne Gowda A
    Replies:
    2
    Views:
    885
    Karl Heinz Buchegger
    Oct 31, 2003
  2. Replies:
    2
    Views:
    377
    Kai-Uwe Bux
    Feb 5, 2006
  3. Replies:
    1
    Views:
    363
    Victor Bazarov
    Feb 17, 2006
  4. Hicham Mouline
    Replies:
    0
    Views:
    437
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    421
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page