#include <iostream>
using namespace std;
#define MAXSIZE 128
class Stack{
public:
Stack();
Stack(int, int*);
Stack(Stack& );
~Stack();
Stack& operator=(Stack&);
void push(int);
bool pop(int&);
friend ostream& operator<<(ostream&, Stack&);
friend istream& operator>>(istream&, Stack&);
private:
int max;
int count;
int *mem;
};
Stack::Stack(){
max=MAXSIZE;
count=0;
mem=new int [MAXSIZE];
}
Stack::Stack(int size, int *p){
max=MAXSIZE;
count=size;
mem=new int [MAXSIZE];
for (int i=0; i<size; i++)
mem[i]=p[i];
}
Stack::Stack(Stack& s){
max=s.max;
count=s.count;
mem=new int[max];
for( int i=0; i<count; i++)
mem[i]=s.mem[i];
}
Stack::~Stack(){
delete[] mem;
}
Stack& Stack::operator=(Stack& s){
if (this!=&s){
delete[] mem;
max=s.max;
count=s.count;
mem=new int[max];
for(int i=0; i<count; i++)
mem[i]=s.mem[i];
}
return *this;
}
void Stack::push(int a){
if(count-1<max){
mem[count]=a;
count++;
}
}
bool Stack::pop(int& a){
if(count>0){
a=mem[count-1];
count--;
return true;
}
return false;
}
ostream& operator<<(ostream& os, Stack& a){
int b;
if (a.pop(b))
os<<b<<endl;
else
os<<«Stack pust.»<<endl;
return os;
}
istream& operator>>(istream& is, Stack& a){
int b;
is >> b;
a.push (b);
return is;
}
---------------вар1
int main(){
Stack *a = new Stack ();
cin >> *a;
cout << *a;
return 0;
}
-------------вар2
int main(){
Stack a();
cin >> a;
cout << a;
return 0;
}
Там стек, при вводе/выводе происходит push/pop соответственно.
Вар1 компилируется и работает, а вар2 - нет, выдает портянку ошибок:
ts.cc: In function ‘int main()’:
ts.cc:93:7: error: ambiguous overload for ‘operator>>’ in ‘std::cin >> a’
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/istream:120:7: note: candidates are: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match>
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/istream:124:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& (*)(std::basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>, std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios<char>] <near match>
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/istream:131:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match>
ts.cc:94:8: warning: the address of ‘Stack a()’ will always evaluate as ‘true’
Вопрос: что каждая из них означает и где я не прав?

Ответ на:
комментарий
от yoghurt

Ответ на:
комментарий
от yoghurt

Ответ на:
комментарий
от yoghurt

Ответ на:
комментарий
от Dimanc

Ответ на:
комментарий
от yoghurt

Ответ на:
комментарий
от Yareg

Ответ на:
комментарий
от yoghurt

Ответ на:
комментарий
от Dimanc

Ответ на:
комментарий
от yoghurt

Ответ на:
комментарий
от Dimanc

Ответ на:
комментарий
от yoghurt

Ответ на:
комментарий
от Dimanc

Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.
Похожие темы
- Форум C++ STL: ostream_iterator (2006)
- Форум временные обьекты (2004)
- Форум istringstream не пашет (2004)
- Форум Вывод типа для значений для которых нет перегрузки в std::ostream (2019)
- Форум Хочу c++11 в gcc - не работает (2013)
- Форум int stopwatch::stop() (2004)
- Форум C++ , Шаблоны, ошибка линкера LNK2019 (2008)
- Форум Пробшемма с шаблонами в C++ (2006)
- Форум iostream? (2003)
- Форум проблема при запуске .срр файла через команду gcc (2023)