Hi,
I am sure it's silly.
I get the error:
/tmp/ccyVx871.o: In function `main':
UseStack.cpp:(. text+0x8b): undefined reference to `Stack<int>::St ack()'
UseStack.cpp:(. text+0x9e): undefined reference to
`Stack<int>::pu sh(int)'
UseStack.cpp:(. text+0xa9): undefined reference to `Stack<int>::po p()'
UseStack.cpp:(. text+0xea): undefined reference to
`Stack<int>::~S tack()'
UseStack.cpp:(. text+0x105): undefined reference to
`Stack<int>::~S tack()'
collect2: ld returned 1 exit status
when I try to compile following code. If I put main() into Stack.cpp it
works. What am I missing?
(I use g++ 4.1.2)
Thanks!
Max
Stack.h:
-----------------------------
#ifndef _Stack_h
#define _Stack_h
template <class T>
class Stack {
protected:
struct elem{
T value;
elem* next;
};
elem* current;
int count;
public:
void push(T);
T pop();
Stack() ;
~Stack();
};
Stack.cpp:
-----------------------------
#include "Stack.h"
#include<iostre am>
using namespace std;
template <class T>
void Stack<T>::push( T value)
{
elem* e = new elem;
e->value = value;
e->next = Stack::current;
Stack::current = e;
Stack::count++;
}
template <class T>
T Stack<T>::pop()
{
if (Stack::count == 0 ){
cerr << "Error: Stack empty!\n",
exit(-1);
}else{
elem* e = Stack::current;
Stack::current = e->next;
Stack::count--;
T v = e->value;
delete e;
return v;
}
}
template <class T>
Stack<T>::Stack (){
current = 0;
count = 0;
}
template <class T>
Stack<T>::~Stac k(){
while(count 0)
Stack::pop();
}
UseStack.cpp:
-----------------------------
#include "Stack.h"
#include <iostream>
#include <vector>
#include "Stack.h"
using namespace std;
int main(){
Stack<inta;
a.push(5);
std::cout << "Value : " << a.pop() << "\n";
}
I am sure it's silly.
I get the error:
/tmp/ccyVx871.o: In function `main':
UseStack.cpp:(. text+0x8b): undefined reference to `Stack<int>::St ack()'
UseStack.cpp:(. text+0x9e): undefined reference to
`Stack<int>::pu sh(int)'
UseStack.cpp:(. text+0xa9): undefined reference to `Stack<int>::po p()'
UseStack.cpp:(. text+0xea): undefined reference to
`Stack<int>::~S tack()'
UseStack.cpp:(. text+0x105): undefined reference to
`Stack<int>::~S tack()'
collect2: ld returned 1 exit status
when I try to compile following code. If I put main() into Stack.cpp it
works. What am I missing?
(I use g++ 4.1.2)
Thanks!
Max
Stack.h:
-----------------------------
#ifndef _Stack_h
#define _Stack_h
template <class T>
class Stack {
protected:
struct elem{
T value;
elem* next;
};
elem* current;
int count;
public:
void push(T);
T pop();
Stack() ;
~Stack();
};
Stack.cpp:
-----------------------------
#include "Stack.h"
#include<iostre am>
using namespace std;
template <class T>
void Stack<T>::push( T value)
{
elem* e = new elem;
e->value = value;
e->next = Stack::current;
Stack::current = e;
Stack::count++;
}
template <class T>
T Stack<T>::pop()
{
if (Stack::count == 0 ){
cerr << "Error: Stack empty!\n",
exit(-1);
}else{
elem* e = Stack::current;
Stack::current = e->next;
Stack::count--;
T v = e->value;
delete e;
return v;
}
}
template <class T>
Stack<T>::Stack (){
current = 0;
count = 0;
}
template <class T>
Stack<T>::~Stac k(){
while(count 0)
Stack::pop();
}
UseStack.cpp:
-----------------------------
#include "Stack.h"
#include <iostream>
#include <vector>
#include "Stack.h"
using namespace std;
int main(){
Stack<inta;
a.push(5);
std::cout << "Value : " << a.pop() << "\n";
}
Comment