By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,131 Members | 1,922 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,131 IT Pros & Developers. It's quick & easy.

template function specialization problem

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.