使用重载运算符+无需赋值

时间:2015-03-28 18:27:57

标签: c++ operator-overloading

我有一个简单(不简单)的问题:当我们不使用赋值重载运算符时,是否有可能重载operator +将不能正常工作?

示例:

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中的元素会得到一些随机数。为什么呢?

还有一个问题:为什么我不能做那些重载&lt;&lt;和+运营商:

cout << a+b;

编辑:这是代码:

    #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;

我知道这是在我尝试使用operaotr而没有重载的时候,但问题在于运算符&lt;&lt;超载了。

1 个答案:

答案 0 :(得分:0)

是的,这是可能的。

例如:想象一下,+=运算符都会反转一个数字。如果反转两次 - 得到正确的结果。但是如果你只反转一次(例如使用非重载的=运算符),你会得到意想不到的结果。

你的情况发生了什么 - 我不知道。您没有提供足够的数据来提供更具体和有用的答案。

相关问题