Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template methods, declaration and Implementation in header file, andOverloaded methods.

Reply
Thread Tools

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

 
 
anto.anish@gmail.com
Guest
Posts: n/a
 
      06-09-2008
Hi ,

Since, i did not want to write explicit 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



 
Reply With Quote
 
 
 
 
metarox
Guest
Posts: n/a
 
      06-10-2008
If you do a template specialization, you have to tell the compiler
that it's one with "template <>" else the compiler will simply tell
you that your second function is already included in the template
declared version which is why you get that message when he reaches the
second read method. Also you can't partially specialize templated
functions, they need to be fully specialized.

You can also specialize the functions outside of the template file.h.

template<class T>
unsigned long Att::read(T& start, unsigned long count)
{
//Implementation written here
}

template<>
unsigned long Att::read(std::back_insert_iterator<
vector<std::string> >& start, unsigned long count)
{
//Implementation written here
}

David
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
separating class declaration (header file) and implementation (sourcefile) Giovanni Gherdovich C++ 6 08-19-2008 09:28 AM
Template methods, declaration and Implementation in header file andoverloaded methods. anto.anish@gmail.com C++ 0 06-09-2008 08:43 PM
Template methods - avoiding instantiation, declaration andimplementation in header file anto.anish@gmail.com C++ 0 06-09-2008 08:39 PM
Include implementation file in in the header file for template class Kouisawang C++ 5 10-25-2006 08:07 PM



Advertisments