Sign in to follow this  
OneThreeThreeSeven

templates don't work

Recommended Posts

error LNK2019: unresolved external symbol "public: bool __thiscall Lister<int>::pop(int &)" (?pop@?$Lister@H@@QAE_NAAH@Z) referenced in function _main 1>MainProgram.obj : error LNK2019: unresolved external symbol "public: bool __thiscall Lister<int>::isempty(void)const " (?isempty@?$Lister@H@@QBE_NXZ) referenced in function _main
//lister.cpp	
        #include "Lister.h"
	#include <iostream>
	using namespace std;

	template <typename T>
	Lister<T>::Lister() // create an empty stack
	{
	top = 0;
	}
	
	template <typename T>
	bool Lister<T>::isempty() const
	{
	return top == 0;
	}
	
	template <typename T>
	bool Lister<T>::isfull() const
	{
	return top == MAX;
	}
	
	template <typename T>
	bool Lister<T>::push(const T & item)
	{
	if (top < MAX)
	{
	items[top++] = item;
	return true;
	}
	else
	return false;
	}
	
	template <typename T>
	bool Lister<T>::pop(T & item)
	{
	if (top > 0)
	{
	item = items[--top];
	return true;
	}
	else
	return false;
	}

//lister.h
#ifndef LISTER_H_
#define LISTER_H_
	
	template <typename T>
	class Lister
	{
	private:
	enum {MAX = 10}; // constant specific to class
	T items[MAX]; // holds stack items
	int top; // index for top stack item
	int countPush;
	public:
	Lister();
	bool isempty() const;
	bool isfull() const;
	// push() returns false if stack already is full, true otherwise
	bool push(const T &); // add item to stack
	// pop() returns false if stack already is empty, true otherwise
	bool pop(T &); // pop top into item
	};
	
#endif

//mainprogram.cpp
	#include <iostream>
	#include <cctype> 
	#include "Lister.h"


	int main()
	{
	using namespace std;

	Lister<int> st; // create an empty stack
	char ch;
	int po;
	cout << "Please enter A to add a purchase order,\n" << "P to process a PO, " << " E to edit stack, " << " or Q to quit.\n";

	while (cin >> ch && toupper(ch) != 'Q')
	{
	while (cin.get() != '\n')
	continue;
	if (!isalpha(ch))
	{
	cout << '\a';
	continue;
	}

		switch(ch)
	{
		case 'A':
		case 'a': cout << "Enter a PO number to add: ";
		cin >> po;
		if (st.isfull())
	cout << "stack already full\n";
	else
	st.push(po);
	break;
	case 'P':
	case 'p': if (st.isempty())
	cout << "stack already empty\n";
	else 
	{
	st.pop(po);
	cout << "PO #" << po << " popped\n";
	}
	break;
		}
	cout << "Please enter A to add a purchase order,\n" << "P to process a PO, or Q to quit.\n";
	}
	cout << "Bye\n";
	return 0;
	}

Those two errors at the top...I get a bunch of those when I try to compile this. I'm just teting out templates. Anyone know the cause for this? O_o

Share this post


Link to post
Share on other sites
You can't hide the definition of templated functions in a .cpp file. Put it all in the header file. This way, when compiling some .cpp, since all it will see is the header, the compiler can correctly generate code for instantiated template types. If you hide it in a different .cpp, the compiler can't see it

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this