Jun 27, 2022 at 7:44am UTC
Hello, I'm trying to create a stack wherein it can accept any data types, but somehow I'm having some errors while running the program.
I keep on getting the error LNK2019 and LNK1120.
It would be great if you could help me with this, thank you!
the code:
//Main.cpp File
#include <iostream>
#include <string>
#include "Data.h"
using namespace std;
int main()
{
IntStack<string> stack(5);
string catchVar;
cout << "Pushing hello\n";
stack.push("hello");
cout << "Pushing good\n";
stack.push("good");
cout << "Pushing morning\n";
stack.push("morning");
cout << "Pushing afternoon\n";
stack.push("Afternoon");
cout << "Pushing evening\n";
stack.push("Evening");
cout << "Popping...\n";
stack.pop(catchVar);
cout << catchVar << endl;
stack.pop(catchVar);
cout << catchVar << endl;
stack.pop(catchVar);
cout << catchVar << endl;
stack.pop(catchVar);
cout << catchVar << endl;
stack.pop(catchVar);
cout << catchVar << endl;
}
//Implementation.cpp file
#include <iostream>
#include "Data.h"
using namespace std;
template <typename T>
IntStack<T>::IntStack(int size)
{
stackArray = new T[size];
stackSize = size;
top = -1;
}
template <typename T>
void IntStack<T>::push(T num)
{
if (isFull())
{
cout << "The stack is full.\n";
}
else
{
top++;
stackArray[top] = num;
}
}
template <typename T>
void IntStack<T>::pop(T& num)
{
if (isEmpty())
{
cout << "The stack is empty.\n";
}
else
{
num = stackArray[top];
top--;
}
}
template <typename T>
bool IntStack<T>::isFull()
{
bool status;
if (top == stackSize - 1)
status = true;
else
status = false;
return status;
}
template <typename T>
bool IntStack<T>::isEmpty()
{
bool status;
if (top == -1)
status = true;
else
status = false;
return status;
}
//data.h file
#pragma once
template<typename T>
class IntStack
{
private:
T* stackArray;
int stackSize;
int top;
public:
IntStack(int size);
void push(T num);
void pop(T& num);
bool isFull();
bool isEmpty();
};
Last edited on Jun 27, 2022 at 7:50am UTC
Jun 27, 2022 at 8:38am UTC
If a class/struct is templated, it can't be split between header and implementation files. The whole class/struct has to be coded in the same header file.
Jun 27, 2022 at 8:54am UTC
As one file, consider:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#include <iostream>
#include <string>
template <typename T>
class IntStack {
private :
T* stackArray {};
int stackSize {};
int top {-1};
public :
IntStack(unsigned size);
~IntStack() { delete [] stackArray; }
IntStack(const IntStack&) = delete ;
IntStack& operator =(const IntStack&) = delete ;
void push(const T& num);
void pop(T& num);
bool isFull() const ;
bool isEmpty() const ;
};
template <typename T>
IntStack<T>::IntStack(unsigned size) : stackArray(new T[size]), stackSize(size) {}
template <typename T>
void IntStack<T>::push(const T& num) {
if (isFull())
std::cout << "The stack is full.\n" ;
else
stackArray[++top] = num;
}
template <typename T>
void IntStack<T>::pop(T& num) {
if (isEmpty())
std::cout << "The stack is empty.\n" ;
else
num = stackArray[top--];
}
template <typename T>
bool IntStack<T>::isFull() const {
return top == stackSize - 1;
}
template <typename T>
bool IntStack<T>::isEmpty() const {
return top == -1;
}
int main() {
IntStack<std::string> stack(5);
std::string catchVar;
std::cout << "Pushing hello\n" ;
stack.push("hello" );
std::cout << "Pushing good\n" ;
stack.push("good" );
std::cout << "Pushing morning\n" ;
stack.push("morning" );
std::cout << "Pushing afternoon\n" ;
stack.push("Afternoon" );
std::cout << "Pushing evening\n" ;
stack.push("Evening" );
std::cout << "Popping...\n" ;
stack.pop(catchVar);
std::cout << catchVar << '\n' ;
stack.pop(catchVar);
std::cout << catchVar << '\n' ;
stack.pop(catchVar);
std::cout << catchVar << '\n' ;
stack.pop(catchVar);
std::cout << catchVar << '\n' ;
stack.pop(catchVar);
std::cout << catchVar << '\n' ;
}
Pushing hello
Pushing good
Pushing morning
Pushing afternoon
Pushing evening
Popping...
Evening
Afternoon
morning
good
hello
Last edited on Jun 27, 2022 at 8:57am UTC