Hi everyone,
I am having a problem while trying to do an operator overload of operator<<
I am getting:
Stack.cpp:96: `std::ostream& Stack::operator<<(std::ostream&, const Stack&)'
must take exactly one argument
Stack.cpp:96: no `std::ostream& Stack::operator<<(std::ostream&, const Stack&)'
member function declared in class `Stack'
I can't seem to find why I am getting this. Can anyone help me? Thank you in advance. Here is my code:
#include <iostream>
using namespace std;
#include "Stack.h"
Stack::Stack(void)
: _size(5), arr(new int[_size]), top(-1)
{
;;;
}
Stack::Stack(const Stack &s)
{
copy(s);
}
Stack::~Stack(void)
{
delete []arr;
}
void Stack::push(const int foo)
{
if (top >= (_size -1))
{
int nsize = _size + 5;
int *narr = new int[nsize];
for (int i=0; i<=top; ++i)
{
narr[i] = arr[i];
}
delete []arr;
_size = nsize;
arr = narr;
}
arr[++top] = foo;
}
int Stack::pop(void)
{
int rval = peek();
--top;
return rval;
}
int Stack::peek(void) const
{
int rval = 0;
if (empty())
{
cerr << "peek/pop when empty" << endl;
exit(0);
}
else
{
rval = arr[top];
}
return rval;
}
int Stack::size(void) const
{
return (top + 1);
}
bool Stack::empty(void) const
{
return (top < 0);
}
void Stack::copy(const Stack &s)
{
_size = s._size;
top = s.top;
arr = new int[_size];
for(int i=0; i<=top; ++i)
{
arr[i] = s.arr[i];
}
}
const Stack & Stack::operator= (const Stack &s)
{
// if (this != s)
// {
// delete []arr;
copy(s);
// }
return *this;
}
ostream & Stack::operator<< (ostream &ost, const Stack &s)
{
for (int i=0; i<=s.top; ++i)
{
ost << s.arr[i] << ' ';
}
return ost;
}
#include <iostream>
using namespace std;
// template <class X>
class Stack
{
private:
int _size;
int *arr;
int top;
public:
Stack(void);
~Stack(void);
Stack(const Stack &);
void push(const int);
int pop(void);
int peek(void) const;
int size(void) const;
bool empty(void) const;
void copy(const Stack &);
const Stack & operator= (const Stack &);
friend ostream & operator<< (ostream &, const Stack &);
// bool operator!= (const Stack &) const;
};