У меня есть простой (не простой) вопрос: есть ли вероятность того, что перегруженный оператор + будет работать не так, как мы хотим, когда мы не будем использовать перегруженный оператор assingment?Использование перегруженного оператора + без оценки
Пример:
object a, b, c;
//Here is some code where I add some numbers to object a and b;
c=a+b;
cout << c;
И дело: когда я использую перегруженный оператор assingment, все нормально. Но без этого элементы, находящиеся в c, получают некоторые случайные числа. Зачем?
И еще один вопрос: почему я не могу сделать что-то подобное с перегруженных < < и + операторы:
cout << a+b;
EDIT: Вот код:
#include <iostream>
#include <cstdio>
using namespace std;
class List;
class element{
private:
int number;
element* next;
friend class List;
friend ostream & operator<<(ostream &, List &);
public:
element(int data){
number=data;
}
int getData(){
return number;
}
};
class List{
private:
element* head;
int size;
friend ostream & operator<<(ostream &, List &);
public:
List(){
head=NULL;
size=0;
}
List(const List &lista){
//cout << "wyw" << endl;
//fflush(stdout);
if (lista.size==0){
size=0;
head=NULL;
}
else
{
size=0;
head=NULL;
element* tmp=lista.head;
while(tmp!=NULL){
cout << tmp->number << " ";
fflush(stdout);
this->addToList(tmp->number);
tmp=tmp->next;
}
}
}
~List(){
if (head!=NULL){
element* tmp=head;
element *temp;
while(tmp!=NULL){
temp=tmp->next;
delete tmp;
tmp=temp;
}
head=NULL;
}
}
void addToList(int data){
element* newNode=new element(data);
newNode->next=NULL;
element *tmp=head;
if (tmp!=NULL){
while(tmp->next!=NULL){
tmp=tmp->next;
}
tmp->next=newNode;
}
else{
head=newNode;
}
size++;
}
List& operator=(const List& lista){
if (&lista==this) return *this;
if (this->head!=NULL){
element* tmp=head;
element *temp;
while(tmp!=NULL){
temp=tmp->next;
delete tmp;
tmp=temp;
}
head=NULL;
}
if (lista.size==0){
size=0;
head=NULL;
}
else
{
size=0;
head=NULL;
element* tmp=lista.head;
while(tmp!=NULL){
this->addToList(tmp->number);
tmp=tmp->next;
}
}
return *this;
}
List operator+(const List &lista){
List newList;
element *tmp=this->head;
while (tmp!=NULL){
newList.addToList(tmp->number);
tmp=tmp->next;
}
tmp=lista.head;
while (tmp!=NULL){
newList.addToList(tmp->number);
tmp=tmp->next;
}
return newList;
}
};
istream & operator>>(istream &str, List &lists){
int data;
str >> data;
lists.addToList(data);
return str;
}
ostream & operator<<(ostream &str, List &lists){
element*tmp=lists.head;
if (tmp==NULL){
str << "List is empty" << endl;
}
else{
while(tmp!=NULL){
str << tmp->number << "--";
tmp=tmp->next;
}
str << "NULL" << endl << "Size of lsit: " << lists.size << endl;
}
return str;
}
int main(){
List lista;
cin >> lista;
cin >> lista;
List list2=lista;
cout << lista;
cout << list2;
List c;
//cout << list2+lista; CANT DO THAT, WHY?
c=list2+lista;
cout << c;
return 0;
}
@DrewDormann Вот ошибка :
lista.cpp: In function ‘int main()’:
lista.cpp:167:7: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘List’)
cout << lista+list2;
^
lista.cpp:167:7: note: candidates are:
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.cpp:1:
/usr/include/c++/4.8/ostream:108:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(__ostream_type& (*__pf)(__ostream_type&))
^
/usr/include/c++/4.8/ostream:108:7: note: no known conversion for argument 1 from ‘List’ to ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}’
/usr/include/c++/4.8/ostream:117:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>; std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
operator<<(__ios_type& (*__pf)(__ios_type&))
^
/usr/include/c++/4.8/ostream:117:7: note: no known conversion for argument 1 from ‘List’ to ‘std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}’
/usr/include/c++/4.8/ostream:127:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(ios_base& (*__pf) (ios_base&))
^
/usr/include/c++/4.8/ostream:127:7: note: no known conversion for argument 1 from ‘List’ to ‘std::ios_base& (*)(std::ios_base&)’
/usr/include/c++/4.8/ostream:166:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long __n)
^
/usr/include/c++/4.8/ostream:166:7: note: no known conversion for argument 1 from ‘List’ to ‘long int’
/usr/include/c++/4.8/ostream:170:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned long __n)
^
/usr/include/c++/4.8/ostream:170:7: note: no known conversion for argument 1 from ‘List’ to ‘long unsigned int’
/usr/include/c++/4.8/ostream:174:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(bool __n)
^
/usr/include/c++/4.8/ostream:174:7: note: no known conversion for argument 1 from ‘List’ to ‘bool’
In file included from /usr/include/c++/4.8/ostream:609:0,
from /usr/include/c++/4.8/iostream:39,
from lista.cpp:1:
/usr/include/c++/4.8/bits/ostream.tcc:91:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]
basic_ostream<_CharT, _Traits>::
^
/usr/include/c++/4.8/bits/ostream.tcc:91:5: note: no known conversion for argument 1 from ‘List’ to ‘short int’
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.cpp:1:
/usr/include/c++/4.8/ostream:181:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned short __n)
^
/usr/include/c++/4.8/ostream:181:7: note: no known conversion for argument 1 from ‘List’ to ‘short unsigned int’
In file included from /usr/include/c++/4.8/ostream:609:0,
from /usr/include/c++/4.8/iostream:39,
from lista.cpp:1:
/usr/include/c++/4.8/bits/ostream.tcc:105:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits<char>]
basic_ostream<_CharT, _Traits>::
^
/usr/include/c++/4.8/bits/ostream.tcc:105:5: note: no known conversion for argument 1 from ‘List’ to ‘int’
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.cpp:1:
/usr/include/c++/4.8/ostream:192:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned int __n)
^
/usr/include/c++/4.8/ostream:192:7: note: no known conversion for argument 1 from ‘List’ to ‘unsigned int’
/usr/include/c++/4.8/ostream:201:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long long __n)
^
/usr/include/c++/4.8/ostream:201:7: note: no known conversion for argument 1 from ‘List’ to ‘long long int’
/usr/include/c++/4.8/ostream:205:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned long long __n)
^
/usr/include/c++/4.8/ostream:205:7: note: no known conversion for argument 1 from ‘List’ to ‘long long unsigned int’
/usr/include/c++/4.8/ostream:220:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(double __f)
^
/usr/include/c++/4.8/ostream:220:7: note: no known conversion for argument 1 from ‘List’ to ‘double’
/usr/include/c++/4.8/ostream:224:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(float __f)
^
/usr/include/c++/4.8/ostream:224:7: note: no known conversion for argument 1 from ‘List’ to ‘float’
/usr/include/c++/4.8/ostream:232:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long double __f)
^
/usr/include/c++/4.8/ostream:232:7: note: no known conversion for argument 1 from ‘List’ to ‘long double’
/usr/include/c++/4.8/ostream:245:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(const void* __p)
^
/usr/include/c++/4.8/ostream:245:7: note: no known conversion for argument 1 from ‘List’ to ‘const void*’
In file included from /usr/include/c++/4.8/ostream:609:0,
from /usr/include/c++/4.8/iostream:39,
from lista.cpp:1:
/usr/include/c++/4.8/bits/ostream.tcc:119:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
basic_ostream<_CharT, _Traits>::
^
/usr/include/c++/4.8/bits/ostream.tcc:119:5: note: no known conversion for argument 1 from ‘List’ to ‘std::basic_ostream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}’
lista.cpp:141:12: note: std::ostream& operator<<(std::ostream&, List&)
ostream & operator<<(ostream &str, List &lists){
^
lista.cpp:141:12: note: no known conversion for argument 2 from ‘List’ to ‘List&’
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.cpp:1:
/usr/include/c++/4.8/ostream:548:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
^
/usr/include/c++/4.8/ostream:548:5: note: template argument deduction/substitution failed:
lista.cpp:167:16: note: cannot convert ‘List::operator+(const List&)((*(const List*)(& list2)))’ (type ‘List’) to type ‘const unsigned char*’
cout << lista+list2;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.cpp:1:
/usr/include/c++/4.8/ostream:543:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
^
/usr/include/c++/4.8/ostream:543:5: note: template argument deduction/substitution failed:
lista.cpp:167:16: note: cannot convert ‘List::operator+(const List&)((*(const List*)(& list2)))’ (type ‘List’) to type ‘const signed char*’
cout << lista+list2;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.cpp:1:
/usr/include/c++/4.8/ostream:530:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
^
/usr/include/c++/4.8/ostream:530:5: note: template argument deduction/substitution failed:
lista.cpp:167:16: note: cannot convert ‘List::operator+(const List&)((*(const List*)(& list2)))’ (type ‘List’) to type ‘const char*’
cout << lista+list2;
^
In file included from /usr/include/c++/4.8/ostream:609:0,
from /usr/include/c++/4.8/iostream:39,
from lista.cpp:1:
/usr/include/c++/4.8/bits/ostream.tcc:321:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
^
/usr/include/c++/4.8/bits/ostream.tcc:321:5: note: template argument deduction/substitution failed:
lista.cpp:167:16: note: cannot convert ‘List::operator+(const List&)((*(const List*)(& list2)))’ (type ‘List’) to type ‘const char*’
cout << lista+list2;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.cpp:1:
/usr/include/c++/4.8/ostream:513:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
^
/usr/include/c++/4.8/ostream:513:5: note: template argument deduction/substitution failed:
lista.cpp:167:16: note: mismatched types ‘const _CharT*’ and ‘List’
cout << lista+list2;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.cpp:1:
/usr/include/c++/4.8/ostream:493:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
^
/usr/include/c++/4.8/ostream:493:5: note: template argument deduction/substitution failed:
lista.cpp:167:16: note: cannot convert ‘List::operator+(const List&)((*(const List*)(& list2)))’ (type ‘List’) to type ‘unsigned char’
cout << lista+list2;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.cpp:1:
/usr/include/c++/4.8/ostream:488:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
^
/usr/include/c++/4.8/ostream:488:5: note: template argument deduction/substitution failed:
lista.cpp:167:16: note: cannot convert ‘List::operator+(const List&)((*(const List*)(& list2)))’ (type ‘List’) to type ‘signed char’
cout << lista+list2;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.cpp:1:
/usr/include/c++/4.8/ostream:482:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
operator<<(basic_ostream<char, _Traits>& __out, char __c)
^
/usr/include/c++/4.8/ostream:482:5: note: template argument deduction/substitution failed:
lista.cpp:167:16: note: cannot convert ‘List::operator+(const List&)((*(const List*)(& list2)))’ (type ‘List’) to type ‘char’
cout << lista+list2;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.cpp:1:
/usr/include/c++/4.8/ostream:476:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
^
/usr/include/c++/4.8/ostream:476:5: note: template argument deduction/substitution failed:
lista.cpp:167:16: note: cannot convert ‘List::operator+(const List&)((*(const List*)(& list2)))’ (type ‘List’) to type ‘char’
cout << lista+list2;
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from lista.cpp:1:
/usr/include/c++/4.8/ostream:471:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
^
/usr/include/c++/4.8/ostream:471:5: note: template argument deduction/substitution failed:
lista.cpp:167:16: note: deduced conflicting types for parameter ‘_CharT’ (‘char’ and ‘List’)
cout << lista+list2;
^
In file included from /usr/include/c++/4.8/string:52:0,
from /usr/include/c++/4.8/bits/locale_classes.h:40,
from /usr/include/c++/4.8/bits/ios_base.h:41,
from /usr/include/c++/4.8/ios:42,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from lista.cpp:1:
/usr/include/c++/4.8/bits/basic_string.h:2753:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/include/c++/4.8/bits/basic_string.h:2753:5: note: template argument deduction/substitution failed:
lista.cpp:167:16: note: ‘List’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
cout << lista+list2;
Я знаю, что это когда я пытаюсь использовать raotr без перегрузки, но здесь проблема в том, что оператор < < перегружен.
Можете ли вы показать нам этот код, который не работает для вас? –
@DrewDormann у вас есть это здесь. – oszust002
Вы получаете ошибку компилятора, я полагаю, из 'cout << list2 + lista;'. И, может быть, вы не знаете, что означает ошибка? –