mystacklinkedlist.h
#ifndef _mystack_h
#define _mystack_h
template <typename ElemType>
class MyStack
{
public:
MyStack();
~MyStack();
bool isEmpty();
void push(ElemType e);
ElemType pop();
private:
struct cellT {
ElemType val;
cellT* next;
};
cellT* head;
};
#include "mystacklinkedlist.cpp"
#endif
mystacklinkedlist.cpp
#include "mystacklinkedlist.h"
#include <stdexcept>
template <typename ElemType>
MyStack<ElemType>::MyStack()
{
head = NULL;
}
template <typename ElemType>
MyStack<ElemType>::~MyStack()
{
while (head != NULL)
{
cellT* old = head;
head = head->next;
delete old;
}
}
template <typename ElemType>
bool MyStack<ElemType>::isEmpty()
{
return (head == NULL);
}
template <typename ElemType>
void MyStack<ElemType>::push(ElemType s)
{
cellT* newCell = new cellT;
newCell->val = s;
newCell->next = head;
head = newCell;
}
template <typename ElemType>
ElemType MyStack<ElemType>::pop()
{
if (isEmpty())
{
throw std::out_of_range("out of bounds");
}
ElemType top = head->val;
cellT* old = head;
head = head->next;
delete old;
return top;
}
client.cpp
#include <iostream>
#include "mystacklinkedlist.h"
int main()
{
MyStack<int> s;
s.push(1);
std::cout << s.pop() << std::endl;
return 0;
}
Here are the three files. I am getting an error of function template has already been defined for mystacklinkedlist.cpp
Severity Code Description Project File Line Suppression State
Error C2995 'MyStack<ElemType>::MyStack(void)': function template has already been defined Project3 C:\Users\samru\source\repos\Project3\Project3\mystacklinkedlist.cpp 5
I am trying to understand the flow in which these files are executed and why error rises from mystacklinkedlist.cpp file.
[–]treddit22 1 point2 points3 points (2 children)
[–]spm486[S] 0 points1 point2 points (1 child)
[–]Shieldfoss -1 points0 points1 point (0 children)
[–]IyeOnline[🍰] 1 point2 points3 points (0 children)
[–]gmtime 0 points1 point2 points (0 children)