template function specialization problem

B

Bari

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 T> class A;
template <class T> A<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>();
}
 
A

Adrian Hawryluk

Bari said:
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 T> class A;
template <class T> A<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/by-nc-sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top