【问题标题】:I'm getting a long, weird error when I pushback a class object to a initialized class vector当我将一个类对象推回一个初始化的类向量时,我遇到了一个很长很奇怪的错误
【发布时间】:2020-11-13 14:38:24
【问题描述】:

我是面向对象编程的新手。我正在尝试制作一款名为 Hex 的游戏。我使用 Hex 对象的静态向量来保存活动游戏(可以有多个活动游戏)。但是每当我尝试将新游戏推回十六进制向量时,我都会收到这个奇怪的错误:

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>&)’

这是我的代码(问题出在课程之后的 addNewGame() 函数中。当我删除 games.push_back(game) 时,没有错误。):

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

【问题讨论】:

  • 请张贴minimal代码来说明问题。您发布的 90% 的代码与错误无关。
  • 尝试将games.push_back(game) 更改为games.push_back(std::move(game));。复制game 是不可能的,而且无论如何都会低效,因为它会被销毁。
  • 不是答案,但您的 GCC 是古老的。考虑更新。
  • @HolyBlackCat 我知道而且我讨厌它。但是我的学校强迫我们使用这个。

标签: c++ oop c++11 vector push-back


【解决方案1】:

推回vector包含复制ob对象。禁止复制std::ofstream

变量ofstream saveFile;似乎只在函数Hex::saveGame中使用,所以你应该在函数Hex::loadGame中将变量声明为局部变量,如ifstream loadFile;,而不是成员变量。

【讨论】:

  • 非常感谢!我已经尝试了 3 个小时不同的东西:D 这很有效。
  • “推回向量包含对象的复制。” — 不一定。如果提供了非抛出移动语义,则推回右值不会复制。
【解决方案2】:

您的hex 类有一个saveFile 类型为std::ofstream 的成员。由于 std::ofstream 有一个已删除的复制构造函数,Hex 也变得不可复制构造,这就是为什么 push_back 不能添加 Hex 对象。

您可以通过将saveFile 设为引用成员(即std::ofstream&amp;)来解决此问题,这将使Hex 再次可复制构造。但是,您需要在 member-initializer-list 中初始化此成员。但是由于在构造 Hex 对象时您不知道文件的名称,因此该方法将不起作用。

由于saveFile 似乎只在saveGame 中使用,一个简单的解决方法是将其声明为该函数中的局部变量:

void Hex::saveGame(const string& fileName){
    ofstream saveFile;
    saveFile.open(fileName);
    // ...
}

这是demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-25
    • 2021-07-31
    • 2022-11-20
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-22
    相关资源
    最近更新 更多