简体   繁体   English

我不明白这个错误

[英]I dont understand this error

Im currently writing an array wrapper in c++ for a class and I have no idea what the error im getting means. 我目前正在用c ++为一个类编写一个数组包装器,我不知道该错误意味着什么。

These are the source and header file 这些是源文件和头文件

#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_ */

And the main 和主要

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

Now this is the error I'm getting and I don't understand it 现在这是我得到的错误,我不明白

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)

{
      ^

Write the operator the following way 通过以下方式写运算符

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

Class ostream does not have public copy constructor. ostream类没有公共副本构造函数。 So the first parameter has to have a reference type. 因此,第一个参数必须具有引用类型。

Also it would be better to declare the second parameter as a constant reference 另外最好将第二个参数声明为常量引用

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

Take into account that this operator has a bug in case when the size of the list is equal to 0 that is this statement 考虑到当列表的大小等于0时,该运算符存在一个错误,即该语句

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

is invalid. 是无效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM