473,545 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with Template Specialization

Hi,
For a while I have had some problem understanding just how template
specialisation works in certain cases. In abridged form my code looks
like this;

--MyTemplate.h--
#ifndef MyTemplateH
#define MyTemplateH

template <class Type>
class MyTemplate
{
public:
void MyMethod(void);
};

#include "MyTemplate.inl "

#endif

--MyTemplate.inl--
#include <iostream>
#include <string>

template <class Type>
void MyTemplate<Type >::MyMethod(voi d)
{
std::cout << "MyTemplate<Typ e>::MyMethod" << std::endl;
}

template<>
void
MyTemplate<bool >::MyMethod
(void)
{
std::cout << "MyTemplate<boo l>::MyMethod" << std::endl;
}

--main.cpp--
#include <iostream>
#include "MyTemplate .h"
//#include "MyClass.h"

using namespace std;

int main(void)
{
cout << "Template Specialisation Test" << endl;

MyTemplate<doub le> myDoubleTemplat e;
MyTemplate<bool > myBoolTemplate;

myDoubleTemplat e.MyMethod();
myBoolTemplate. MyMethod();

//MyClass myClass;
//myClass.MyOther Method();
}

If I compile and run this (gcc on cygwin);
g++ main.cpp
../a

I get exactly what I expect:
Template Specialisation Test
MyTemplate<Type >::MyMethod
MyTemplate<bool >::MyMethod

However, if I uncomment the MyClass stuff and compile in the relevant
files;

--MyClass.h--
#ifndef MyClassH
#define MyClassH

class MyClass
{
public:
void MyOtherMethod(v oid);
};

#endif

--MyClass.cpp--
#include "MyClass.h"
#include "MyTemplate .h"
#include <iostream>

void MyClass::MyOthe rMethod(void)
{
std::cout << "MyClass::MyOth erMethod" << std::endl;

MyTemplate<doub le> myClassDoubleTe mplate;
myClassDoubleTe mplate.MyMethod ();

MyTemplate<bool > myClassBoolTemp late;
myClassBoolTemp late.MyMethod() ;
}

I get this;

$ g++ main.cpp MyClass.cpp
/cygdrive/c/DOCUME~1/sjs1/LOCALS~1/Temp/cc4Op3z4.o(.tex t+0x0):MyClass. cpp:
multi
ple definition of `MyTemplate<boo l>::MyMethod() '
/cygdrive/c/DOCUME~1/sjs1/LOCALS~1/Temp/ccPG3TEU.o(.tex t+0x0):main.cpp :
first de
fined here
collect2: ld returned 1 exit status

Now, I can understand this, given that the .inl file is included in
both places - but I have tried moving stuff the specialisation or the
template method or both into seperate .cpp files, with absolutely no
success; any ideas how to resolve this?

Stephen.

Sep 2 '05 #1
2 1437

Stephen Starkie wrote:
[snip]

template <class Type>
void MyTemplate<Type >::MyMethod(voi d)
{
std::cout << "MyTemplate<Typ e>::MyMethod" << std::endl;
}

template<>
void
MyTemplate<bool >::MyMethod
(void)
{
std::cout << "MyTemplate<boo l>::MyMethod" << std::endl;
}

[snip]

$ g++ main.cpp MyClass.cpp
/cygdrive/c/DOCUME~1/sjs1/LOCALS~1/Temp/cc4Op3z4.o(.tex t+0x0):MyClass. cpp:
multi
ple definition of `MyTemplate<boo l>::MyMethod() '
/cygdrive/c/DOCUME~1/sjs1/LOCALS~1/Temp/ccPG3TEU.o(.tex t+0x0):main.cpp :
first de
fined here
collect2: ld returned 1 exit status

You can try this, it should work (use the 'inline' keyword):

template<>
inline void
MyTemplate<bool >::MyMethod
(void)
{
std::cout << "MyTemplate<boo l>::MyMethod" << std::endl;

}

Hth, -shez-

Sep 2 '05 #2
Thankyou; esp. so quickly

I feel like an idiot; I should have known this! Sometimes it just
takes someone to look over your shoulder!

Stephen

Sep 2 '05 #3

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

Similar topics

17
6829
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section 18.3.1, 'Iseq'). I want the version for containers that he gives, but also to provide a specialization for construction from a pair<It,It> (eg because...
2
2526
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the template for your type. However, this requires you to copy the whole template, and change the method, which leads to code duplication... If there's only 1...
8
7655
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I specialize Music<Bach>, I expect that the original play() method is available in the specialization, but it is not. How can I fix this? -X
2
5765
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem which I can't figure out. I have a simple class C with 2 template member objects, and a function print() that prints the value of these objects. I...
6
2006
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit instantiation of foo:
2
1771
by: Yang Zhang | last post by:
I have a small program like this: //////////////////////////////////////////////// #include <iostream> using namespace std ; // set bits in an address template <typename T> inline T* set_addr(const T* addr, unsigned long n) {
9
2750
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second assign() function really a specialization of the first assign() or is it an assign() overload? Thank you. -- Marek
2
2477
by: Thomas Kowalski | last post by:
Hi, I would like to write a template class Polygon<VertexTypthere vertex typ can be eigther a pointer or a value typ. It has an attribute: std::vector<VertexTypvertices; And a methode: VertexValueTyp& vertex(int i); that dereferences vertices internally in case VertexTyp is a pointer
2
7677
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
6
2589
by: abir | last post by:
i have a template as shown template<typename Sclass Indexer{}; i want to have a specialization for std::vector both const & non const version. template<typename T,typename Aclass Indexer<std::vector<T,A {} matches only with nonconst version. anyway to match it for both? and get if it is const or nonconst? Actually i want 2 specialization,...
0
7935
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...
1
7449
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7780
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6009
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...
1
5351
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...
0
5069
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...
0
3479
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...
1
1911
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
1
1037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.