473,756 Members | 3,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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::str ing&
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::str ing&
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_ite rator<
vector<std::str ing& 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(vect or <std::string>:: iterator& start,
vector <std::string>:: iterator& stop)
{
//Implementation written here
}
}
#endif

Jun 27 '08 #1
1 2777
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_ite rator<
vector<std::str ing& start, unsigned long count)
{
//Implementation written here
}

David
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2168
by: Nomak | last post by:
Hello all, i have two template classes which needs each other. I tried to write some fwd decl / decl / impl in a good way but i can't get it to compile. Explanations: - .hh files are for declaration (template or not) - .hxx files are template impl
2
3986
by: Ruben Campos | last post by:
I have a problem with a template function that is declared as a friend of a template class. I'll first show the exact problem with source code: // MyClass.hpp template <typename T> class MyClass { // ... friend void MyFriendFunction (MyClass <T> * const ptrMyClass); // ...
2
2637
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh class BaseClass {
3
1407
by: Veeru | last post by:
i have one structure SPageDetails. i have a template class CStack. Now i have a pointer of this stack class in another class "CAnotherClass" as: class CAnotherClass { CStack<SPageDetails*m_pStack_PageManager; };
14
11744
by: Jess | last post by:
Hello, I was told that if I have a template class or template function, then the definitions must be put into the header file where I put the declarations. On the other hand, it is generally good to put source code and declaration into separate files (.h head filer and .cpp source file). What can I do to template functions/classes? Do I have to put them all into one header file? Thanks,
13
6597
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm trying to use from an executable, compile using gcc 4.1.2. Everything works fine until I try specializing one of the static member function templates in a non-template class. I have a feeling I'm messing up something obvious so before I post a...
8
1751
by: Tim Frink | last post by:
Hi, I want to use a callback function together with templates. Let's say I've this code: File a.h: class A { private:
0
1354
by: anto.anish | last post by:
Hi , Since, i did not want to write all instantiations in Source file of all template methods for various different datatypes that my client might use, Instead, 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__
0
1993
by: anto.anish | last post by:
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__
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8712
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7244
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5141
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5303
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.