Reputation: 43
I am attempt to write a stack which uses nodes to store data and am I attempting to use it with a template so that the data stored can be a different datatype. I am using it with a stack so the node can be moved or deleted however I have initalised the stack and am trying to receive feedback by couting text to make sure everything is working and it is not.
No feedback will be given back to template and there are no errors in Visual Studios 2013. IF someone could take a look and explain why that would be great. Also if anyone has more information on using 'Generic Programming' to allow for multiple datatypes that would be very helpful. Below is the code separated in two headers and one main cpp.
template <class T>
class Node {
public:
T data;
Node * next;
Node * start;
Node(T inputdata){
data = inputdata;
}
T getData() {
return data;
}
};
#include "node.h"
using namespace std;
template<typename DT>
class Stack{
private:
int size;
Node<DT> *top;
Node<DT> *s_ptr;
public:
Stack();
void push(DT val);
DT pop();
void start();
};
template<typename DT>
Stack<DT>::Stack()
{
cout << "Stack Started" << endl;
}
template<typename DT>
void Stack<DT>::push(DT val)
{
}
template<typename DT>
DT Stack<DT>::pop()
{
}
template<typename DT>
void Stack<DT>::start()
{
cout << "Started" << endl;
}
#include "stdafx.h"
#include <iostream>
#include "stack.h"
template <typename DT>
void start(){
Stack<DT> * stack = new Stack<DT>();
stack->start();
}
int _tmain(int argc, _TCHAR* argv[])
{
void start();
system("pause");
return 0;
}
I think the reason why it won't initalise is because in the main class it says void start within main when it should just be start(). However if I remove that I get No instance of function template start matches the template list. Someone care to explain?
Upvotes: 0
Views: 42
Reputation: 171393
I think the reason why it won't initalise is because in the main class it says void start within main when it should just be start().
Correct. void start();
doesn't call a function, it declares a function, so all you've done is declare something, and not run any code.
However if I remove that I get No instance of function template start matches the template list.
You declared start
as a function template taking a single template parameter, so start()
is not a function, it's a template. How is the compiler supposed to know what the template argument should be?
You need to do something like:
start<int>();
Upvotes: 5