当我将 class 对象推回到初始化的 class 向量时,出现了一个很长的奇怪错误
I'm getting a long, weird error when I pushback a class object to a initialized class vector
我是面向对象编程的新手。我正在尝试制作一款名为 Hex 的游戏。我使用十六进制对象的静态向量来保存活动游戏(可以有不止一个活动游戏)。但是每当我尝试将新游戏推回到 Hex vector 时,我都会收到这个奇怪的错误:
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>&)’
这是我的代码(问题出在 [=19= 之后的 addNewGame() 函数中)。当我删除 games.push_back(游戏)时,没有错误。):
#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);
}
推回 vector 包含复制 ob 对象。禁止复制 std::ofstream
。
似乎变量ofstream saveFile;
只在函数Hex::saveGame
中使用,所以你应该在函数Hex::loadGame
中像ifstream loadFile;
一样声明变量为局部变量作为成员变量。
您的 hex
class 有一个类型为 std::ofstream
的成员 saveFile
。由于 std::ofstream
具有删除的复制构造函数,因此 Hex
也变得不可复制构造,这就是为什么 push_back
无法添加 Hex
对象。
您可以通过使 saveFile
成为参考成员来解决此问题,即 std::ofstream&
,这将使 Hex
再次可复制构造。但是,您需要在成员初始化器列表中初始化该成员。但是由于在构造 Hex
对象时不知道文件的名称,因此该方法不起作用。
因为 saveFile
似乎只在 saveGame
中使用,一个简单的解决方法是在该函数中将其声明为局部变量:
void Hex::saveGame(const string& fileName){
ofstream saveFile;
saveFile.open(fileName);
// ...
}
这里是 demo。
我是面向对象编程的新手。我正在尝试制作一款名为 Hex 的游戏。我使用十六进制对象的静态向量来保存活动游戏(可以有不止一个活动游戏)。但是每当我尝试将新游戏推回到 Hex vector 时,我都会收到这个奇怪的错误:
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>&)’
这是我的代码(问题出在 [=19= 之后的 addNewGame() 函数中)。当我删除 games.push_back(游戏)时,没有错误。):
#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);
}
推回 vector 包含复制 ob 对象。禁止复制 std::ofstream
。
似乎变量ofstream saveFile;
只在函数Hex::saveGame
中使用,所以你应该在函数Hex::loadGame
中像ifstream loadFile;
一样声明变量为局部变量作为成员变量。
您的 hex
class 有一个类型为 std::ofstream
的成员 saveFile
。由于 std::ofstream
具有删除的复制构造函数,因此 Hex
也变得不可复制构造,这就是为什么 push_back
无法添加 Hex
对象。
您可以通过使 saveFile
成为参考成员来解决此问题,即 std::ofstream&
,这将使 Hex
再次可复制构造。但是,您需要在成员初始化器列表中初始化该成员。但是由于在构造 Hex
对象时不知道文件的名称,因此该方法不起作用。
因为 saveFile
似乎只在 saveGame
中使用,一个简单的解决方法是在该函数中将其声明为局部变量:
void Hex::saveGame(const string& fileName){
ofstream saveFile;
saveFile.open(fileName);
// ...
}
这里是 demo。