Template specialization

R

ratzeel

Hi,

I've two modules in my project. One is a dll and another is an exe.

The dll contains the following template code in one of its header
file.

#ifndef __STACK_HEADER__
#define __STACK_HEADER__

template< class T>
class Stack
{
public:
void push(const T& item);

private:
T arr[MAX];
};

// specialization for int
template<>
void Stack<int>::push(const int& item)
{

// push ints..

}

// specialization for float
template<>
void Stack<float>::push(const float& item)
{

// push floats..
}

#endif // __STACK_HEADER__

Another module, an exe loads the above dll.

The exe has got two .cpp files.

1.cpp:

void func_1()
{
Stack<int> stack;
stack.push(10);

Stack<float> stack1;
stack1.push(210);
}

2.cpp:

void func_2()
{
Stack<int> stack;
stack.push(10);

Stack<float> stack1;
stack1.push(210);
}

When I build this exe, I'm getting the following errors.

reader1 error LNK2005: "public: void __thiscall Stack<int>::push(int
const &)" (?push@?$Stack@H@@QAEXABH@Z) already defined in reader1.obj

reader1 error LNK2005: "public: void __thiscall
Stack<float>::push(float const &)" (?push@?$Stack@M@@QAEXABM@Z)
already defined in reader1.obj


Both the exe and the dll are built with /MDd (multi threaded debug
DLL) option.

Any thoughts on how to resolve this multiply defined symbols error
when we specialize.

Thanks....
 
R

ratzeel

Hi,

I've two modules in my project. One is a dll and another is an exe.

The dll contains the following template code in one of its header
file.

#ifndef __STACK_HEADER__
#define __STACK_HEADER__

template< class T>
class Stack
{
public:
void push(const T& item);

private:
T arr[MAX];

};

// specialization for int
template<>
void Stack<int>::push(const int& item)
{

// push ints..

}

// specialization for float
template<>
void Stack<float>::push(const float& item)
{

// push floats..

}

#endif // __STACK_HEADER__

Another module, an exe loads the above dll.

The exe has got two .cpp files.

1.cpp:

void func_1()
{
Stack<int> stack;
stack.push(10);

Stack<float> stack1;
stack1.push(210);

}

2.cpp:

void func_2()
{
Stack<int> stack;
stack.push(10);

Stack<float> stack1;
stack1.push(210);

}

When I build this exe, I'm getting the following errors.

reader1 error LNK2005: "public: void __thiscall Stack<int>::push(int
const &)" (?push@?$Stack@H@@QAEXABH@Z) already defined in reader1.obj

reader1 error LNK2005: "public: void __thiscall
Stack<float>::push(float const &)" (?push@?$Stack@M@@QAEXABM@Z)
already defined in reader1.obj

Both the exe and the dll are built with /MDd (multi threaded debug
DLL) option.

Any thoughts on how to resolve this multiply defined symbols error
when we specialize.

Thanks....

Well.. The culprit seems to be the missing function body for the push
function in the Stack template class.

The solution is

template< class T>
class Stack
{
public:
void push(const T& item) { }

private:
T arr[10];
};
 
V

V.R. Marinov

ratzeel said:
I've two modules in my project. One is a dll and another is an exe.

The dll contains the following template code in one of its header
file.

Another module, an exe loads the above dll.

The exe has got two .cpp files.


When I build this exe, I'm getting the following errors.

reader1 error LNK2005: "public: void __thiscall Stack<int>::push(int
const &)" (?push@?$Stack@H@@QAEXABH@Z) already defined in reader1.obj

reader1 error LNK2005: "public: void __thiscall
Stack<float>::push(float const &)" (?push@?$Stack@M@@QAEXABM@Z)
already defined in reader1.obj

Forget it.
You can't export uninstatiated templates.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top