c++ - Using overloaded operator+ without assingment -


i have simple(not simple) question: possibility overloaded operator+ working not want when won't use assingment overloaded operator?

example:

object a, b, c; //here code add numbers object , b; c=a+b; cout << c; 

and thing is: when i'm using overloaded assingment operator, fine. without this, elements in c gets random numbers. why?

and question: why can't overloaded << , + operators:

cout << a+b; 

edit: here code:

    #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 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 that, why? c=list2+lista; cout << c; return 0; } 

@drewdormann here error:

    lista.cpp: in function ‘int main()’: lista.cpp:167:7: error: no match ‘operator<<’ (operand types ‘std::ostream {aka std::basic_ostream<char>}’ , ‘list’)   cout << lista+list2;        ^ lista.cpp:167:7: note: candidates are: in file included /usr/include/c++/4.8/iostream:39:0,                  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 argument 1 ‘list’ ‘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 argument 1 ‘list’ ‘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 argument 1 ‘list’ ‘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 argument 1 ‘list’ ‘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 argument 1 ‘list’ ‘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 argument 1 ‘list’ ‘bool’ in file included /usr/include/c++/4.8/ostream:609:0,                  /usr/include/c++/4.8/iostream:39,                  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 argument 1 ‘list’ ‘short int’ in file included /usr/include/c++/4.8/iostream:39:0,                  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 argument 1 ‘list’ ‘short unsigned int’ in file included /usr/include/c++/4.8/ostream:609:0,                  /usr/include/c++/4.8/iostream:39,                  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 argument 1 ‘list’ ‘int’ in file included /usr/include/c++/4.8/iostream:39:0,                  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 argument 1 ‘list’ ‘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 argument 1 ‘list’ ‘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 argument 1 ‘list’ ‘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 argument 1 ‘list’ ‘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 argument 1 ‘list’ ‘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 argument 1 ‘list’ ‘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 argument 1 ‘list’ ‘const void*’ in file included /usr/include/c++/4.8/ostream:609:0,                  /usr/include/c++/4.8/iostream:39,                  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 argument 1 ‘list’ ‘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 argument 2 ‘list’ ‘list&’ in file included /usr/include/c++/4.8/iostream:39:0,                  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’) type ‘const unsigned char*’   cout << lista+list2;                 ^ in file included /usr/include/c++/4.8/iostream:39:0,                  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’) type ‘const signed char*’   cout << lista+list2;                 ^ in file included /usr/include/c++/4.8/iostream:39:0,                  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’) type ‘const char*’   cout << lista+list2;                 ^ in file included /usr/include/c++/4.8/ostream:609:0,                  /usr/include/c++/4.8/iostream:39,                  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’) type ‘const char*’   cout << lista+list2;                 ^ in file included /usr/include/c++/4.8/iostream:39:0,                  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*’ , ‘list’   cout << lista+list2;                 ^ in file included /usr/include/c++/4.8/iostream:39:0,                  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’) type ‘unsigned char’   cout << lista+list2;                 ^ in file included /usr/include/c++/4.8/iostream:39:0,                  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’) type ‘signed char’   cout << lista+list2;                 ^ in file included /usr/include/c++/4.8/iostream:39:0,                  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’) type ‘char’   cout << lista+list2;                 ^ in file included /usr/include/c++/4.8/iostream:39:0,                  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’) type ‘char’   cout << lista+list2;                 ^ in file included /usr/include/c++/4.8/iostream:39:0,                  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 parameter ‘_chart’ (‘char’ , ‘list’)   cout << lista+list2;                 ^ in file included /usr/include/c++/4.8/string:52:0,                  /usr/include/c++/4.8/bits/locale_classes.h:40,                  /usr/include/c++/4.8/bits/ios_base.h:41,                  /usr/include/c++/4.8/ios:42,                  /usr/include/c++/4.8/ostream:38,                  /usr/include/c++/4.8/iostream:39,                  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’ not derived ‘const std::basic_string<_chart, _traits, _alloc>’   cout << lista+list2; 

i know when i'm trying use operaotr without overloading, here problem operator << overloaded.

yes, possible.

for example: imagine both + , = operators invert number. if invert twice - correct result. if invert once (e.g. using non-overloaded = operator) unexpected result.

what happening in case - have no idea. provide not enough data more concrete , useful answer.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -