I'm trying to pass a sequence of chars to a linked list, but this error came up to me, and I have no idea what it could be.
Cannot convert 'Type' to 'const ChainNode<Type>'
Here is what I have:
#include <iostream>
using namespace std;
template <class Type> class Chain;
template <class Type> class ChainNode;
template <class Type>
class Stack
{
friend class Chain <Type>;
public:
Stack(int MaxStackSize = DefaultSize);
//~Stack();
bool Full();
bool Empty();
Type& Top();
void Push(const Type& item);
Type Pop(Type&);
void Print();
void Read(char x[], int size, Chain <Type> input_chain);
private:
int MaxSize;
int top;
Type* stack;
char a;
};
I'm having problems on this function, which it purpose does not matter actually, because the error is not related to the algorithm, is something about arguments on classes on C++.
template <class Type>
void Stack<Type>::Read(char x[], int size, Chain <Type> input_chain) {
int count = 0;
for (int i = 0; i < size; i++)
{
count++;
if (Empty()) {
if (x[i] == '+' or x[i] == '*') {
Push(x[i]);
break;
}
else{
cout << x[i];
input_chain.attach(x[i]); //<-- Here is the problem
}
}
}
//Node of the chain declaration
template <class Type>
class ChainNode
{
friend class Stack <Type>;
friend class Chain <Type>;
public:
ChainNode() {};
//~ChainNode();
private:
Type character;
ChainNode* link;
};
The error is related to the void function on this class:
//Chain class declaration
template <class Type>
class Chain
{
friend class Stack <Type>;
friend class ChainNode <Type>;
public:
Chain() {
first = 0;
last = 0;
};
void attach(Type& k) {
ChainNode<Type>* newnode = new ChainNode<Type> (k);
if (first == 0) first = last = newnode;
else {
last->link = newnode;
last = newnode;
}
}
private:
ChainNode <Type> *first;
ChainNode <Type> *last;
};
And this is my main function:
int main() {
Stack <char> mystack(20);
Chain <char> mychain;
int const size = 20;
char math_infix[size];
int operators = 0;
int operands = 0;
int math_size = 0;
cout << "Write the math equation: " << endl;
cin >> math_infix;
for (int i = 0; i < size; i++)
{
if (i % 2 != 0 and (math_infix[i] == '+' or math_infix[i] == '*')) {
operators++;
}
}
operands = operators + 1;
math_size = operands + operators;
mystack.Read(math_infix, math_size, mychain);
//mystack.Print();
return 0;
}
You need to provide a converting constructor in ChainNode
for the following line in attach
to work:
ChainNode<Type>* newnode = new ChainNode<Type> (k);
You need to add:
ChainNode(Type c) : character(c) {}
to your ChainNode
Class.