Im getting a bunch of errors from the compiler.Not sure what im doing wrong....

Header File

#include<iostream>
using namespace std;
#ifndef STACK_H
#define STACK_H

template<class stackElem, int maxSize=80>
class stack
{

public:
stack()
{
top=-1;
}

bool push(const stackElem &item);


};
#include "stack.cpp"
#endif

this is the .cpp file

#include "stack.h"
using namespacestd;

template<class stackElem,int maxSize>
bool stack<stackElem,maxSize>::push(const stackElem &item)
{
if(isfull())
return false;
elements[top]=item;
top++;
return true;
}

Hi,

You havent mentioned what error you are getting.
But I am assuming that isFull() is there and you will be able to fix the normal errors like using namespacestd; should be acutlaly using namespace std; and top and elements need to be declared.

Still you gonna face linker issues.
You can solve liker issues in several ways:

FIRST WAY) Keep everything in header. This is the most preferred way. Here is a working code where I have removed cpp file and moved the non inline member function to header file. Most easiest way to get rid of linker issues. But may create huge executables. But for your purpose (student/homework), I would suggest that you always include your member functions in the header file.

#ifndef STACK_H
#define STACK_H

#include<iostream>
using namespace std;


template<class stackElem, int maxSize=80>
class stack
{
	int top;
	stackElem elements[maxSize];

public:
	stack()
	{
		top=-1;
	}

	bool push(const stackElem &item);
	bool isfull(){return top == maxSize;}
};

//#include "stack.cpp"

template<class stackElem, int maxSize>
bool stack<stackElem,maxSize>::push(const stackElem &item)
{
	if(isfull())
		return false;
	elements[top]=item;
	top++;
	return true;
}


#endif /* STACK_H_ */

SECOND WAY: If you want to put your member functions in cpp file anyway, still you can do it like below:

//stack.hpp
#include<iostream>
using namespace std;


template<class stackElem, int maxSize=80>
class stack
{
	int top;
	stackElem elements[maxSize];

public:
	stack()
	{
		top=-1;
	}

	bool push(const stackElem &item);
	bool isfull(){return top == maxSize;}
};
//stack.cpp file.


#include "stack.h"
using namespace std;


template<class stackElem, int maxSize>
bool stack<stackElem,maxSize>::push(const stackElem &item)
{
	if(isfull())
		return false;
	elements[top]=item;
	top++;
	return true;
}


template class stack<int, 80>; //NOTICE THIS

Only problem in the second type is, every time you want to instantiate a new type of stack, you need to include that type in the cpp file, like above, we have done it for int.

If you can not change the cpp file(part of some library), you can still do it like below:
Create a second cpp file and include the actual one in it, and instantiate all the types.

//stack-imp.cpp
#include "stack.cpp"
template class stack<int, 80>; //NOTICE THIS
template class stack<float, 80>; //NOTICE THIS

THIRD WAY: use keyword export. actually i am not going to give example of export here, as in c++0x export is going to be removed, though it will still be a keyword.
and also not all the compiler support export yet.

Happy coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.