Jump to content
  • Advertisement
Sign in to follow this  
OneThreeThreeSeven

templates don't work

This topic is 3193 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!