I have a problem with this code. How can i call the compare function to compare 2 different types !!! It gives me an error message:
Main.cpp
(23): error C2664: 'Stack<int>::compare' : cannot convert parameter 1 from 'Stack<StackElement>' to 'Stack<StackElement>'
with
[
StackElement=char
]
and
[
StackElement=int
]
Stack.h:
======
Code:
#ifndef STACK
#define STACK
const int STACK_CAPACITY=128;
#include<assert.h>
template<typename StackElement>
class Stack
{
public:
Stack();
bool empty() const;
void push(const StackElement&);
void display() const;
StackElement top() const;
void pop();
bool compare(Stack<StackElement> S1, Stack<StackElement> S2);
//bool compare(Stack<StackElement> S1);
friend ostream& operator <<(ostream&, const Stack<StackElement>&);
private:
StackElement myArray[STACK_CAPACITY];
int myTop;
};
template<typename StackElement>
Stack<StackElement>::Stack()
{
myTop=-1;
}
template<typename StackElement>
bool Stack<StackElement>::empty() const
{
return (myTop==-1);
}
template<typename StackElement>
void Stack<StackElement>::push( const StackElement& value)
{
if (myTop<STACK_CAPACITY-1)
{
++myTop;
myArray[myTop]=value;
}
else
cerr<<"*** Stack is full---can't add new vale***\n";
}
template<typename StackElement>
void Stack<StackElement>::display() const
{
for (int i=myTop;i>=0;i--)
cout<<myArray[i]<<endl;
}
template<typename StackElement>
StackElement Stack<StackElement>::top() const
{
assert (!empty());
return myArray[myTop];
}
template<typename StackElement>
void Stack<StackElement>::pop()
{
if (myTop>=0)
myTop--;
else
cerr<<"*** Stack is empty--can't remove a value ***\n";
}
template<typename StackElement>
ostream& operator<<(ostream& out, const Stack<StackElement> & st)
{
for(int pos=st.myTop; pos>=0; pos--)
out<<st.myArray[pos]<<endl;
return out;
}
template<typename StackElement>
bool Stack<StackElement>::compare(Stack<StackElement> S1, Stack<StackElement> S2)
{
// code to compare the two array types
return 0;
}
#endif
Main.cpp:
=======
Code:
#include<iostream>
using namespace std;
#include"Stack.h"
int main()
{
Stack<int> s1;
Stack<char> s2;
char str[]="ABCDEFG";
for(int i=0;i<4;i++) s1.push(i);
cout<<"Stack s1 contents\n";
s1.display();
cout<<"Stack s1 Top value: "<<s1.top();
for(i=0;i<4;i++) s2.push(str[i]);
cout<<"\n\nStack s2 contents\n";
s2.display();
cout<<"Stack s2 Top value: "<<s2.top()<<endl<<endl;
s1.compare(s1,s2);
cout<<s1<<s2;
cout<<endl;
return 0;
}