473,396 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

template function specialization problem

Hi,

When i do the following specializition which commented in the source then
including the header in two cpp files gives me an error:
test3.obj : error LNK2005: "class A<int* __cdecl makeA<int>(void)"
(??$makeA@H@@YAPAV?$A@H@@XZ) already defined in testSTL.obj

Can you please tell me how i can specialize the template function and still
use the header in multiple cpp files.

Thank you in advance!

Bar

//HEADER
#pragma once

template <class Tclass A;
template <class TA<T>* makeA();

template <typename T>
class A{
private:
A(){}
T _a;
public:
friend A *makeA<>(); //error in g++-3.4, works with g++3.3
};

template <typename T>
A<T>* makeA(){
return new A<T>;
}

//SPECIALIZATION GIVING PROBLEM
template <>
A<int>* makeA(){
return new A<int>;
}
//END PROBLEM SPECIALIZATION

// end header

//CPP

int _tmain(int argc, _TCHAR* argv[])
{
A<int>* a = makeA<int>();
}

Mar 17 '07 #1
1 2118
Bari wrote:
Hi,

When i do the following specializition which commented in the source then
including the header in two cpp files gives me an error:
test3.obj : error LNK2005: "class A<int* __cdecl makeA<int>(void)"
(??$makeA@H@@YAPAV?$A@H@@XZ) already defined in testSTL.obj

Can you please tell me how i can specialize the template function and still
use the header in multiple cpp files.

Thank you in advance!

Bar

//HEADER
#pragma once

template <class Tclass A;
template <class TA<T>* makeA();

template <typename T>
class A{
private:
A(){}
T _a;
public:
friend A *makeA<>(); //error in g++-3.4, works with g++3.3
};

template <typename T>
A<T>* makeA(){
return new A<T>;
}

//SPECIALIZATION GIVING PROBLEM
template <>
A<int>* makeA(){
return new A<int>;
}
//END PROBLEM SPECIALIZATION

// end header

//CPP

int _tmain(int argc, _TCHAR* argv[])
{
A<int>* a = makeA<int>();
}
I tried your code, but I couldn't get it to compile with _tmain(). I'm
not familiar with using TCHAR so, it is probably something that was not
included. Replacing tmain with a regular main signature, it compiled
under g++ 3.4.4 without problem, using it in a header that you gave.

I would try doing a clean then remake your source. I would say it is
probably some phase error. That or a bug in g++ 3.4 ;).
Adrian

--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under the Creative Commons /
\ Attribution-Noncommercial-Share Alike 3.0 License /
\_____[http://creativecommons.org/licenses/...sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
Mar 18 '07 #2

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

Similar topics

17
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...
6
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something...
7
by: CoolPint | last post by:
While I was testing my understanding of Functioin Template features by playing with simple function templates, I got into a problem which I cannot understand. I would be very grateful if someone...
2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
6
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...
8
by: Massimiliano Alberti | last post by:
Can I specialize a template function in a subclass without overriding it? (the main template function is defined in a base class). Now I'm doing something like that: (in base class)...
5
by: Amit | last post by:
Greetings all, I am writing some code somehwat similar to the test code I have below. I am having a variety of issues with template specialization. I am not sure if this is related to something...
5
by: Niklas Norrthon | last post by:
I've been banging my head in the wall for some time now over a little problem having to do with partial specialization of function templates. The real problem is more complex than this, but...
9
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...
2
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:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.