2015-05-07 2 views
0

Im в настоящее время пишет оболочку массива в C++ для класса, и я понятия не имею, что означает ошибка im im.Я не понимаю эту ошибку

Это исходный файл и заголовок

#include "List.h" 
#include <cstdlib> 
#include <stdexcept> 
#include <iostream> 
using namespace std; 

List::List(int x){ 
    if(x>10){ 
     arrSize=x; 
    }else{ 
     arrSize=10; 
    } 
    array = new int[arrSize]; 
} 
List::List(List& list){ 
    array = new int[list.size()]; 
    arrSize = list.size(); 
    for(int x=0;x<arrSize;x++){ 
     array[x]=list[x]; 
    } 
} 
List::~List(){ 
    for(int x=0;x<arrSize;x++){ 
     if(array[x]){ 
      delete(&array[x]); 
     } 
    } 
} 
int List::size(){ 
    return arrSize; 
} 

int& List::operator [](int& index){ 
    if(index>arrSize-1){ 
     throw out_of_range("Array Index Out of Bounds"); 
    }else{ 
     return array[index]; 
    } 
} 
int& List::operator [](const int& index){ 
    if(index>arrSize-1){ 
     throw out_of_range("Array Index Out of Bounds"); 
    }else{ 
     return array[index]; 
    } 
} 
List& List::operator +(List& list){ 
    List *retList = new List(list.size()+arrSize); 
    for(int x=0;x<arrSize;x++){ 
     (*retList)[x]=array[x]; 
    } 
    for(int x=arrSize;x<list.size()+arrSize;x++){ 
     (*retList)[x]=list[x]; 
    } 
    return *retList; 
} 
void List::operator =(List& list){ 
    delete(array); 
    arrSize=list.size(); 
    array = new int[arrSize]; 
    for(int x=0;x<arrSize;x++){ 
     array[x]=list[x]; 
    } 
} 


#ifndef LIST_H_ 
#define LIST_H_ 
#include <iostream> 
using namespace std; 


class List{ 
private: 
    int arrSize; 
    int *array; 
public: 
    List(int x=10); 
    List(List&); 
    ~List(); 
    int size(); 
    void operator=(List&); 
    List& operator+(List&); 
    int& operator[](const int&); 
    int& operator[](int&); 

}; 
void operator << (ostream io,List& list){ 
    io << "{"; 
    for(int x=0;x<list.size()-1;x++){ 
     io << list[x] + ","; 
    } 
    io << list[list.size()-1]+"}"; 
} 
#endif /* LIST_H_ */ 

И главный

#include "List.h" 
#include <iostream> 
using namespace std; 

int main(){ 
    List list; 
    for(int x=0;x<10;x++){ 
     list[x]=x; 
    } 
    cout<<list; 
    return 0; 
} 

Теперь это ошибка я получаю, и я не понять это

In file included from /usr/include/c+ 

+/4.8/ios:42:0, 
       from 

/usr/include/c++/4.8/ostream:38, 


from /usr/include/c++/4.8/iostream:39, 


    from List.h:10, 
       from 

Main.cpp:8: 
/usr/include/c++/4.8/bits/ios_base.h: 

In copy constructor 

‘std::basic_ios<char>::basic_ios(const 

std::basic_ios<char>&)’: 
/usr/include/c+ 

+/4.8/bits/ios_base.h:786:5: error: 

‘std::ios_base::ios_base(const std::ios_base&)’ 

is private 
    ios_base(const ios_base&); 
    ^
In 

file included from /usr/include/c+ 

+/4.8/ios:44:0, 
       from 

/usr/include/c++/4.8/ostream:38, 


from /usr/include/c++/4.8/iostream:39, 


    from List.h:10, 
       from 

Main.cpp:8: 
/usr/include/c+ 

+/4.8/bits/basic_ios.h:66:11: error: within this 

context 
    class basic_ios : public ios_base 


    ^
In file included from /usr/include/c+ 

+/4.8/iostream:39:0, 
       from 

List.h:10, 
       from Main.cpp:8: 
/usr/include/c++/4.8/ostream: In copy constructor 

‘std::basic_ostream<char>::basic_ostream(const 

std::basic_ostream<char>&)’: 
/usr/include/c+ 

+/4.8/ostream:58:11: note: synthesized method 

‘std::basic_ios<char>::basic_ios(const 

std::basic_ios<char>&)’ first required here 


class basic_ostream : virtual public 

basic_ios<_CharT, _Traits> 
     ^
Main.cpp: 

In function ‘int main()’: 
Main.cpp:17:8: note: 

synthesized method 

‘std::basic_ostream<char>::basic_ostream(const 

std::basic_ostream<char>&)’ first required here 


cout<<list; 
     ^
In file included from 

Main.cpp:8:0: 
List.h:29:6: error: initializing 

argument 1 of ‘void operator<<(std::ostream, 

List&)’ 
void operator << (ostream io,List& list) 

{ 
    ^
+3

Возьмите ostream по ссылке, IIRC они не могут быть скопированы, вы также должны вернуть поток от вашего оператора, чтобы вы могли цепью звонков на <<. – Borgleader

+2

Рассмотрите возможность чтения [Как написать хороший заголовок?] (Http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title) –

+0

см.: [Как правильно перегрузить оператор << для ostream] (http://stackoverflow.com/questions/476272/how-to-properly-overload-the-operator-for-an-ostream) – NathanOliver

ответ

0

Написать оператору следующим образом

ostream & operator << (ostream &io, List& list){ 
    io << "{"; 
    for(int x=0;x<list.size()-1;x++){ 
     io << list[x] + ","; 
    } 
    io << list[list.size()-1]+"}"; 

    return io; 
} 

класса ostream не имеет публичный конструктор копирования. Поэтому первый параметр должен иметь ссылочный тип.

Кроме того, было бы лучше, чтобы объявить второй параметр в качестве постоянной ссылки

ostream & operator << (ostream &io, const List& list){ 
    io << "{"; 
    for(int x=0;x<list.size()-1;x++){ 
     io << list[x] + ","; 
    } 
    io << list[list.size()-1]+"}"; 

    return io; 
} 

принять во внимание, что этот оператор имеет ошибку в случае, когда размер списка равен 0, что это утверждение

 io << list[list.size()-1]+"}"; 

недействителен.