0

Im currently writing an array wrapper in c++ for a class and I have no idea what the error im getting means.

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)

{
      ^
  • 3
    Take the ostream by reference, IIRC they cannot be copied, you should also return the ostream from your operator so you can chain calls to <<. – Borgleader May 07 '15 at 19:36
  • 2
    Consider reading [How do I write a good title?](http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title) – Matias Cicero May 07 '15 at 19:37
  • see: [How to properly overload the << operator for an ostream](http://stackoverflow.com/questions/476272/how-to-properly-overload-the-operator-for-an-ostream) – NathanOliver May 07 '15 at 19:38
  • In addition, your `List::operator+` is allocating a new list dynamically. There is absolutely no need to do that whatsoever since you have a copy constructor, assignment operator, and destructor. Instead, you're introducing memory leaks by doing what you're doing. Learn how to write a good `operator +` by first writing a good `operator +=` and then write `operator+` in terms of `operator +=`. In addition, your `operator+` should be returning a brand new `List` object *by value*, not by reference. – PaulMcKenzie May 07 '15 at 19:47
  • Your destructor should just be `delete[] array;` There's no need to iterate over the list. – IronMensan May 07 '15 at 19:54
  • See here: http://coliru.stacked-crooked.com/a/e569d35a86baa68b No dynamic allocation necessary. You have copy constructor, assignment operator, and destructor. If those 3 functions are working properly, then use/create your `List` objects just as if you were using `int`, or `double`, etc. – PaulMcKenzie May 07 '15 at 19:56

1 Answers1

0

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. 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

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

is invalid.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335