Template methods, declaration and Implementation in header file andoverloaded methods.

Discussion in 'C++' started by anto.anish@gmail.com, Jun 9, 2008.

  1. Guest

    Hi ,

    Since, i did not want to write instantiations in Source file of all
    template methods for various different datatypes that my client might
    use, i choose to write implementation of template methods along with
    their declarations in the header file. Well, there are also other
    files in the project, which include this header file as well, which
    all gets compiled, linked and tested well.

    #ifndef __ATT_H__
    #define __ATT_H__
    namespace TESTSPACE
    {

    class Att : public Abs
    {

    public:
    template<class T>
    unsigned long read(T& start, unsigned long count=0)
    {
    //Implementation written here
    }
    unsigned long read(std::back_insert_iterator< vector<std::string> >&
    start, unsigned long count=0)
    {
    //Implementation written here
    }


    template<class T>
    unsigned long write(T& start, T& stop)
    {
    //Implementation written here
    }
    unsigned long write(vector <std::string>::iterator& start, vector
    <std::string>::iterator& stop)
    {
    //Implementation written here
    }

    };

    #endif




    However, to keep the header file look more reader friendly with just
    declarations and moving all the implementation towards the end of the
    header file as given below, however generates me errors as
    "error C2373: 'TESTSPACE::Att::read' : redefinition; different type
    modifiers.
    What am i missing here ? The template functions are basically
    overloaded methods, then why does the compiler throw error for the
    below code ?
    #ifndef __ATT_H__
    #define __ATT_H__
    namespace TESTSPACE
    {

    class Att : public Abs
    {

    public:
    template<class T>
    unsigned long read(T& start, unsigned long count=0);
    unsigned long read(std::back_insert_iterator< vector<std::string> >&
    start, unsigned long count=0);


    template<class T>
    unsigned long write(T& start, T& stop);
    unsigned long write(vector <std::string>::iterator& start, vector
    <std::string>::iterator& stop);

    };

    //All implementations done here temporarily
    //Move below code to a separate "impl.h" file later
    template<class T>
    unsigned long Att::read(T& start, unsigned long count)
    {
    //Implementation written here
    }
    unsigned long Att::read(std::back_insert_iterator<
    vector<std::string> >& start, unsigned long count)
    {
    //Implementation written here
    }


    template<class T>
    unsigned long Att::write(T& start, T& stop)
    {
    //Implementation written here
    }
    unsigned long Att::write(vector <std::string>::iterator& start,
    vector <std::string>::iterator& stop)
    {
    //Implementation written here
    }

    #endif
    , Jun 9, 2008
    #1
    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. Kouisawang
    Replies:
    5
    Views:
    600
    Greg Comeau
    Oct 25, 2006
  2. Replies:
    0
    Views:
    257
  3. Replies:
    1
    Views:
    321
    metarox
    Jun 10, 2008
  4. Giovanni Gherdovich
    Replies:
    6
    Views:
    408
    Giovanni Gherdovich
    Aug 19, 2008
  5. mlt
    Replies:
    2
    Views:
    813
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page