jeudi 8 juin 2017

Setting a protected ostream [duplicate]

This question already has an answer here:

I'm working on creating testers for my Item class in a game I've been making, but have some compiler errors that I can't seem to fix.

To test, I want the tester to set the stream that my methods write to (cout by default). To make this work, here is some of the code I've been using:

#ifndef ITEM_H_
#define ITEM_H_

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

class Item {
public:
    Item(string name) { my_name = name; out = cout; }
    string get_desc() const { return my_desc; }
    string get_name() const { return my_name; }
    virtual bool is_book() const { return false; }
    virtual bool is_stationary() const { return false; }
    virtual void read_from(istream& in);
    void set_desc(string desc) { my_desc = desc; }
    virtual void use();
protected:
    string my_name;
    string my_desc;

    // The output that Item methods write to. By default, they write to 
    //cout, but this can be changed for testing.
    ostream& out;

    friend class ItemTester;
};

Item* read_item(istream& in);

#endif /* ITEM_H_ */

Then, later on in the tester class, I set the outputs using the following code:

void ItemTester::test_use() {
    cout << " - Testing use... " << flush;
    ofstream fout("test_file.txt");
    assert( fout.is_open() );
    // Set up items for use
    Item it("Test Item");
    it.out = fout;
    it.set_desc("Test Description");
    Book bk("Test Book");
    bk.out = fout;
    Door dr("Test Door");
    dr.out = fout;
    Multi mul("Test Multi") ;
    mul.out = fout;
    StationaryItem si("Test Stationary");
    si.out = fout;

I don't see what I'm doing wrong. If it helps, here's the compiler's output.

make all 
Building file: ../Door.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Door.d" -MT"Door.o" -o "Door.o" "../Door.cpp"
In file included from ../Location.h:14:0,
                 from ../Door.h:11,
                 from ../Door.cpp:8:
../Item.h: In constructor ‘Item::Item(std::__cxx11::string)’:
../Item.h:19:2: error: uninitialized reference member in ‘std::ostream& {aka class std::basic_ostream<char>&}’ [-fpermissive]
  Item(string name) { my_name = name; out = cout; }
  ^~~~
../Item.h:33:11: note: ‘std::ostream& Item::out’ should be initialized
  ostream& out;
           ^~~
../Item.h:19:44: error: ‘std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator=(const std::basic_ostream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’ is protected within this context
  Item(string name) { my_name = name; out = cout; }
                                            ^~~~
In file included from /usr/include/c++/7.1.1/iostream:39:0,
                 from ../Location.h:11,
                 from ../Door.h:11,
                 from ../Door.cpp:8:
/usr/include/c++/7.1.1/ostream:399:22: note: declared protected here
       basic_ostream& operator=(const basic_ostream&) = delete;
                      ^~~~~~~~
In file included from ../Location.h:14:0,
                 from ../Door.h:11,
                 from ../Door.cpp:8:
../Item.h:19:44: error: use of deleted function ‘std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator=(const std::basic_ostream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’
  Item(string name) { my_name = name; out = cout; }
                                            ^~~~
In file included from /usr/include/c++/7.1.1/iostream:39:0,
                 from ../Location.h:11,
                 from ../Door.h:11,
                 from ../Door.cpp:8:
/usr/include/c++/7.1.1/ostream:399:22: note: declared here
       basic_ostream& operator=(const basic_ostream&) = delete;
                      ^~~~~~~~
make: *** [subdir.mk:44: Door.o] Error 1

Aucun commentaire:

Enregistrer un commentaire