Я пытаюсь заставить эти шаблоны классов в C ++ работать.Но всегда есть эта ошибка.в перегрузке есть какая-то ошибка, но я не знаю что.я попытался перегрузить << оператор, используя функцию-член, но все еще есть ошибка. </p>
#include <iostream>
using namespace std;
const int MAX = 10;
template <class T>
class mstack
{
T stk[MAX];
int top;
public:
mstack()
{
top = -1;
}
void push(T data)
{
if(top==MAX-1)
{
cout << endl << "stack is full" << endl;
}
else
{
top++;
stk[top] = data;
}
}
T pop()
{
if (top==-1)
{
cout << endl << "stack is empty" << endl;
return NULL;
}
else
{
T data = stk[top];
top--;
return data;
}
}
};
class mcomplex
{
float img, real;
public:
mcomplex()
{
real = 0;
img = 0;
}
mcomplex(float r, float i)
{
real = r;
img = i;
}
friend ostream& operator<< (ostream &o,mcomplex &c);
};
ostream& operator<< (ostream &o, mcomplex &c)
{
o << c.real << "\t" << c.img;
return o;
}
int main()
{
mcomplex c1(1.5f,2.5f), c2(3.5f,4.5f), c3(-1.5f,-0.6f);
mstack <mcomplex> s3;
s3.push(c1);
s3.push(c2);
s3.push(c3);
cout << endl << (s3.pop());
cout << endl << s3.pop();
cout << endl << s3.pop() << endl;
return 0;
}
ошибка компилятора выглядит следующим образом:
| 76 | ошибка: нет соответствия для оператора '<< '(типы операндов:' std :: basic_ostream :: __ ostream_type {aka std :: basic_ostream} 'и' mcomplex ') </p>
| 62 | примечание: кандидат: std :: ostream & operator << (std:: ostream &, mcomplex &) </p>
| 77 | ошибка: неверная инициализация неконстантной ссылки типа 'mcomplex &' из значения r типа 'mcomplex'
| 78 | ошибка: нет соответствиядля 'operator <<' (типы операндов: 'std :: basic_ostream :: __ ostream_type {aka std :: basic_ostream}' и 'mcomplex') </p>
Может кто-нибудь показать, в чем здесь ошибка?