0

我全局定义:

vector<player> cUser;

播放器结构如下:

struct player
{
char buffer[256];
string username;
string password;
string version;
int playerID;
int X;
int Y;
int direction;
string chats[5];
unsigned long robotID[3];
int gender;
int securityLevel;
float rep;
int isMember;
int inBattle;
int isFollowed;
int playerLocked;
string currentMap;
string lastHavenMap;
int mapHeight;
int mapWidth;
unsigned long money;
unsigned long points;
int wins;
int losses;
string oldIP;
string newIP;
unsigned long lastOnline;
int layer;
int rank;
int kicked;
void logout();
void refresh();
//other
void startLoops();
int execLoops;
//for the robots
Robots Robot[3];
int robotOut;
int totalRobots;
void addExp(int, int);
void subExp(int, int);
int calcRobotExp(int);
//battles
player *enemyBattleUser;//enemy is a player
npc enemyBattleNpc;//enemy is an npc or boss
Robots rogueRobot;//enemy is a rogue robot
string mainBattleUser;
int battleChoice;//which choice of the battleChoiceType
int battleChoiceType;//0 attack, 1 team, 2 flee
int battleSAD;//short for battleSceneAlgorythmsDone
int battleType;//0-rogue, 1=npc, 2-boss 3-player
int subHealth(int, int, int);
void addHealth(int, int, int);
void battleStart(string, int, int);
void battleEnd(int);
void getMove();
void userTurn(int&, int&, int&, string&, string&, int&);
void enemyTurn(int&, int&, int&, string&, string&, int&);
void extraDamage(int&, string&, string&, int);
void battleScene();
//sockets
int sockID;//main socket id
int cSockID;//chat socket id
void userSockError();
//roam
int rogueBattleCalc();
void warp(string, int, int);
int checkNpcBattle();
void sendInitData();
void roamWorld();
int playerWalk(int);
//chatting
void chatLoop();
void executeCommand(string);
int lastMessageSeen[5];
string chatName[5];
int chatsOpen;
//threads
boost::thread mainThread;
boost::thread chatThread;
};

然后,当我尝试使用以下任何方法调整它的大小时,它给了我一个非常大的错误,我相信它告诉我创建一个构造函数/析构函数,当我这样做时仍然给我同样的错误。

cUser.resize(cUser.size()+1);

player tempUser;
//members of tempUser set here
cUser.push_back(tempUser);

cUser.erase(index);

但是当我以同样的方式调整这个结构的大小时,它工作正常并且它仍然没有构造函数/析构函数,并且它以同样的方式在 cUser 旁边定义。

struct chatbox
{
       //chatbox structure
       string name;
       int locked;//0 no, 1 yes
       int staffOnly;//0 no, 1 yes
       int mType[5];
       int security[5];
       string username[5];
       string message[5];//the higher the index the newer the message
       int messagesNo;
};

任何帮助将不胜感激,谢谢。

错误:

$ make
g++    -c -o server.o server.cpp
In file included from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/i686-pc-cygw
in/bits/c++allocator.h:34:0,
                 from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/allocat
or.h:48,
                 from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/string:43,
                 from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/locale_
classes.h:42,
                 from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/ios_bas
e.h:43,
                 from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/ios:43,
                 from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/istream:40,
                 from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/fstream:40,
                 from server.cpp:5:
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/ext/new_allocator.h: In member fun
ction `void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp
 = player, _Tp* = player*]':
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_vector.h:745:6:   instant
iated from `void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _T
p = player, _Alloc = std::allocator<player>, value_type = player]'
server.cpp:373:32:   instantiated from here
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/ext/new_allocator.h:105:9: error:
no matching function for call to `player::player(const player&)'
user.hpp:81:1: note: candidates are: player::player()
user.hpp:81:1: note:                 player::player(player&)
In file included from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/vector:69:0,

                 from server.cpp:10:
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/vector.tcc: In member functio
n `void std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::iterat
or, const _Tp&) [with _Tp = player, _Alloc = std::allocator<player>, std::vector
<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<player*, std::vector<play
er> >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = player
*]':
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_vector.h:749:4:   instant
iated from `void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _T
p = player, _Alloc = std::allocator<player>, value_type = player]'
server.cpp:373:32:   instantiated from here
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/vector.tcc:306:19: error: no
matching function for call to `player::player(const player&)'
user.hpp:81:1: note: candidates are: player::player()
user.hpp:81:1: note:                 player::player(player&)
In file included from server.hpp:9:0,
                 from server.cpp:15:
/usr/include/boost/thread/detail/thread.hpp: In member function `player& player:
:operator=(player&)':
user.hpp:81:1:   instantiated from `void std::vector<_Tp, _Alloc>::_M_insert_aux
(std::vector<_Tp, _Alloc>::iterator, const _Tp&) [with _Tp = player, _Alloc = st
d::allocator<player>, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_i
terator<player*, std::vector<player> >, typename std::_Vector_base<_Tp, _Alloc>:
:_Tp_alloc_type::pointer = player*]'
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_vector.h:749:4:   instant
iated from `void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _T
p = player, _Alloc = std::allocator<player>, value_type = player]'
server.cpp:373:32:   instantiated from here
/usr/include/boost/thread/detail/thread.hpp:114:17: error: `boost::thread& boost
::thread::operator=(boost::thread&)' is private
user.hpp:81:1: error: within this context
/usr/include/boost/thread/detail/thread.hpp:114:17: error: `boost::thread& boost
::thread::operator=(boost::thread&)' is private
user.hpp:81:1: error: within this context
In file included from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/vector:69:0,

                 from server.cpp:10:
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/vector.tcc: In member functio
n `void std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::iterat
or, const _Tp&) [with _Tp = player, _Alloc = std::allocator<player>, std::vector
<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<player*, std::vector<play
er> >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = player
*]':
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/vector.tcc:312:4: note: synth
esized method `player& player::operator=(player&)' first required here
In file included from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/vector:63:0,

                 from server.cpp:10:
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_construct.h: In function
`void std::_Construct(_T1*, const _T2&) [with _T1 = player, _T2 = player]':
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_uninitialized.h:74:3:   i
nstantiated from `static _ForwardIterator std::__uninitialized_copy<<anonymous>
>::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _I
nputIterator = player*, _ForwardIterator = player*, bool <anonymous> = false]'
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_uninitialized.h:116:46:
 instantiated from `_ForwardIterator std::uninitialized_copy(_InputIterator, _In
putIterator, _ForwardIterator) [with _InputIterator = player*, _ForwardIterator
= player*]'
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_uninitialized.h:318:63:
 instantiated from `_ForwardIterator std::__uninitialized_copy_a(_InputIterator,
 _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator =
player*, _ForwardIterator = player*, _Tp = player]'
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_uninitialized.h:328:24:
 instantiated from `_ForwardIterator std::__uninitialized_move_a(_InputIterator,
 _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = player*,
_ForwardIterator = player*, _Allocator = std::allocator<player>]'
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/vector.tcc:338:8:   instantia
ted from `void std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>:
:iterator, const _Tp&) [with _Tp = player, _Alloc = std::allocator<player>, std:
:vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<player*, std::vect
or<player> >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer =
 player*]'
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_vector.h:749:4:   instant
iated from `void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _T
p = player, _Alloc = std::allocator<player>, value_type = player]'
server.cpp:373:32:   instantiated from here
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_construct.h:80:7: error:
no matching function for call to `player::player(const player&)'
user.hpp:81:1: note: candidates are: player::player()
user.hpp:81:1: note:                 player::player(player&)
<builtin>: recipe for target `server.o' failed
make: *** [server.o] Error 1
4

1 回答 1

4

一个可能的问题是您的player类有boost::thread数据成员,而这些成员是不可复制或可分配的。由于vector::resize()可能需要复制和/或分配,因此编译失败。

于 2012-08-28T16:33:00.457 回答