vendredi 13 novembre 2020

I'm getting a long, weird error when I pushback a class object to a initialized class vector

I am new to object oriented programming. I am trying to make a game called Hex. I use a static vector of Hex objects to hold active games (there can be more then one active game). But whenever I try to pushback a new game to Hex vector, I get this weird error:

In file included from /usr/include/i386-linux-gnu/c++/4.8/bits/c++allocator.h:33:0,
                 from /usr/include/c++/4.8/bits/allocator.h:46,
                 from /usr/include/c++/4.8/string:41,
                 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 HEX.cpp:1:
/usr/include/c++/4.8/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = Hex; _Args = {const Hex&}; _Tp = Hex]’:
/usr/include/c++/4.8/bits/alloc_traits.h:254:4:   required from ‘static typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = Hex; _Args = {const Hex&}; _Alloc = std::allocator<Hex>; typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type = void]’
/usr/include/c++/4.8/bits/alloc_traits.h:393:57:   required from ‘static decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = Hex; _Args = {const Hex&}; _Alloc = std::allocator<Hex>; decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) = <type error>]’
/usr/include/c++/4.8/bits/stl_vector.h:906:34:   required from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Hex; _Alloc = std::allocator<Hex>; std::vector<_Tp, _Alloc>::value_type = Hex]’
HEX.cpp:50:22:   required from here
/usr/include/c++/4.8/ext/new_allocator.h:120:4: error: use of deleted function ‘Hex::Hex(const Hex&)’
  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
    ^
HEX.cpp:7:7: note: ‘Hex::Hex(const Hex&)’ is implicitly deleted because the default definition would be ill-formed:
 class Hex{
       ^
HEX.cpp:7:7: error: use of deleted function ‘std::basic_ofstream<char>::basic_ofstream(const std::basic_ofstream<char>&)’
In file included from HEX.cpp:3:0:
/usr/include/c++/4.8/fstream:599:11: note: ‘std::basic_ofstream<char>::basic_ofstream(const std::basic_ofstream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
     class basic_ofstream : public basic_ostream<_CharT,_Traits>
           ^
/usr/include/c++/4.8/fstream:599:11: error: use of deleted function ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’
In file included from /usr/include/c++/4.8/iostream:39:0,
                 from HEX.cpp:1:
/usr/include/c++/4.8/ostream:58:11: note: ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
     class basic_ostream : virtual public basic_ios<_CharT, _Traits>
           ^
/usr/include/c++/4.8/ostream:58:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
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 HEX.cpp:1:
/usr/include/c++/4.8/bits/basic_ios.h:66:11: note: ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ is implicitly deleted because the default definition would be ill-formed:
     class basic_ios : public ios_base
           ^
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 HEX.cpp:1:
/usr/include/c++/4.8/bits/ios_base.h:792: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 HEX.cpp:1:
/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 HEX.cpp:3:0:
/usr/include/c++/4.8/fstream:599:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
     class basic_ofstream : public basic_ostream<_CharT,_Traits>
           ^
/usr/include/c++/4.8/fstream:599:11: error: use of deleted function ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’
/usr/include/c++/4.8/fstream:72:11: note: ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’ is implicitly deleted because the default definition would be ill-formed:
     class basic_filebuf : public basic_streambuf<_CharT, _Traits>
           ^
In file included from /usr/include/c++/4.8/ios:43:0,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from HEX.cpp:1:
/usr/include/c++/4.8/streambuf:802:7: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’ is private
       basic_streambuf(const basic_streambuf& __sb)
       ^
In file included from HEX.cpp:3:0:
/usr/include/c++/4.8/fstream:72:11: error: within this context
     class basic_filebuf : public basic_streambuf<_CharT, _Traits>
           ^
In file included from /usr/include/c++/4.8/vector:62:0,
                 from HEX.cpp:2:
/usr/include/c++/4.8/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = Hex; _Args = {Hex}]’:
/usr/include/c++/4.8/bits/stl_uninitialized.h:75:53:   required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<Hex*>; _ForwardIterator = Hex*; bool _TrivialValueTypes = false]’
/usr/include/c++/4.8/bits/stl_uninitialized.h:117:41:   required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<Hex*>; _ForwardIterator = Hex*]’
/usr/include/c++/4.8/bits/stl_uninitialized.h:258:63:   required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::move_iterator<Hex*>; _ForwardIterator = Hex*; _Tp = Hex]’
/usr/include/c++/4.8/bits/stl_uninitialized.h:281:69:   required from ‘_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = Hex*; _ForwardIterator = Hex*; _Allocator = std::allocator<Hex>]’
/usr/include/c++/4.8/bits/vector.tcc:415:43:   required from ‘void std::vector<_Tp, _Alloc>::_M_emplace_back_aux(_Args&& ...) [with _Args = {const Hex&}; _Tp = Hex; _Alloc = std::allocator<Hex>]’
/usr/include/c++/4.8/bits/stl_vector.h:911:27:   required from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Hex; _Alloc = std::allocator<Hex>; std::vector<_Tp, _Alloc>::value_type = Hex]’
HEX.cpp:50:22:   required from here
/usr/include/c++/4.8/bits/stl_construct.h:75:7: error: use of deleted function ‘Hex::Hex(Hex&&)’
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^
HEX.cpp:7:7: note: ‘Hex::Hex(Hex&&)’ is implicitly deleted because the default definition would be ill-formed:
 class Hex{
       ^
HEX.cpp:7:7: error: use of deleted function ‘std::basic_ofstream<char>::basic_ofstream(const std::basic_ofstream<char>&)’

And here is my code(problem is in the addNewGame() function right after the class. When I delete games.push_back(game), there is no error.):

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

class Hex{
public:
    Hex() : Hex(6){/*Intentionally empty*/};
    Hex(int x); //boardSize
    inline void setBoardSize(int x){if(x>5) boardSize = x;}
    inline int getBoardSize(){return boardSize;}
    void printBoardSize();
    void saveGame(const string& fileName);
    void loadGame(const string& fileName);
    void printBoard();
    void playGame();
    void play();
    void play(string cellPos,int player); //if player is 1 puts 'x', if it is 2 puts 'o'
    inline void setGameMode(int x){if(x ==1 || x==2) gameMode = x;}
    inline int getGameMode(){return gameMode;}
    static void addNewGame();
    static vector <Hex> games; //holds the active hex games.
private:
    int boardSize;
    int gameMode; //if 1 PVP, else if 2 PVCOMP
    ofstream saveFile;
    class Cell{
    public:
        Cell();
        Cell(int x,int y);
        enum cellState{empty = '.',p1='x',p2='o',p1won='X',p2won='O'};
        void setColumn(int x,Hex game);
        inline int getColumn(){return column;}
        void setRow(int x,Hex game);
        inline int getRow(){return row;}
        inline void setState(cellState x){state=x;}
        inline int getState(){return state;}
    private:
        int column;
        int row;
        cellState state;
    };
    vector< vector<Cell> > hexCells;
};

vector <Hex> Hex::games;
void Hex::addNewGame(){
    Hex game;
    games.push_back(game);
}

Aucun commentaire:

Enregistrer un commentaire