当前位置: 技术问答>linux和unix
求救!undefined reference
来源: 互联网 发布时间:2015-11-29
本文导语: 我自己写了一个类,声明放在taskmgr.h, 定义在taskmgr.cc, 然后在main里面用这个类,但是编译的时候出现了很多undefined reference,都是说我的类没有定义,但是我在写main函数那个文件里已经include "taskmgr.h",请问怎么做...
我自己写了一个类,声明放在taskmgr.h, 定义在taskmgr.cc, 然后在main里面用这个类,但是编译的时候出现了很多undefined reference,都是说我的类没有定义,但是我在写main函数那个文件里已经include "taskmgr.h",请问怎么做才能正确编译呢?
错误具体如下
zzhou@oscar:~/src/IM$ g++ talkserver.cc -I/src/IM -o talkserver
/tmp/ccsniD51.o: In function `thread(void*)':talkserver.cc:(.text+0x6c): undefined reference to `taskMgr::islistening'
:talkserver.cc:(.text+0x3ad): undefined reference to `taskMgr::SendMsg(int, std::basic_string)'
/tmp/ccsniD51.o: In function `main':talkserver.cc:(.text+0x4c6): undefined reference to `taskMgr::SetPortNum(int)'
:talkserver.cc:(.text+0x4ce): undefined reference to `taskMgr::Establish()'
:talkserver.cc:(.text+0x501): undefined reference to `taskMgr::islistening'
:talkserver.cc:(.text+0x541): undefined reference to `taskMgr::relationMap'
:talkserver.cc:(.text+0x5d3): undefined reference to `taskMgr::relationMap'
:talkserver.cc:(.text+0x69e): undefined reference to `taskMgr::Connect(int, sockaddr_in*)'
:talkserver.cc:(.text+0x6bc): undefined reference to `taskMgr::relationMap'
:talkserver.cc:(.text+0x72d): undefined reference to `taskMgr::ReceiveMsg(int)'
:talkserver.cc:(.text+0x7ee): undefined reference to `taskMgr::relationMap'
这是类的声明
class taskMgr
{
public:
static int Establish();
static int GetPortNum();
static void SetPortNum(int port);
static int GetAllConn();
static void HandleDisconnWhenSend(int id);
static map relationMap; //relations between sockets and taskMgr objects ids
static map taskMap; //ids and pointers that pointed to taskMgr
static bool islistening; //if islistening is false then the server quit
private:
static int coutAllConn; //all connections on the server
static int portNum; //listening port number
//maintains the count of taskMgr objects that ever exist,not current actually
static int idTaskMgr;
static int backlog;
public:
enum{TASK_IDLE, TASK_DIE, TASK_BLOCK}; //this used by m_status;
map m_userList; //
char m_recvBuff[MAXTRANSFERSIZE];
string m_sendStr;
private:
int m_status;
int m_conn; //connections that related to this taskMgr
int m_lastsocket; //represent the socket that is working with client
int m_id; //identifier of taskMgr object
public:
taskMgr();
~taskMgr();
bool operator==(const taskMgr mgr)const{return (m_id == mgr.m_id);}
int Connect(int socket, struct sockaddr_in* their_addr);
int ReceiveMsg(int socket);
int SendMsg(int socket, string msg);
void Disconnect(int socket);
int GetStatus();
void SetStatus(int status);
int GetConnections();
int GetLastSocket();
};
错误具体如下
zzhou@oscar:~/src/IM$ g++ talkserver.cc -I/src/IM -o talkserver
/tmp/ccsniD51.o: In function `thread(void*)':talkserver.cc:(.text+0x6c): undefined reference to `taskMgr::islistening'
:talkserver.cc:(.text+0x3ad): undefined reference to `taskMgr::SendMsg(int, std::basic_string)'
/tmp/ccsniD51.o: In function `main':talkserver.cc:(.text+0x4c6): undefined reference to `taskMgr::SetPortNum(int)'
:talkserver.cc:(.text+0x4ce): undefined reference to `taskMgr::Establish()'
:talkserver.cc:(.text+0x501): undefined reference to `taskMgr::islistening'
:talkserver.cc:(.text+0x541): undefined reference to `taskMgr::relationMap'
:talkserver.cc:(.text+0x5d3): undefined reference to `taskMgr::relationMap'
:talkserver.cc:(.text+0x69e): undefined reference to `taskMgr::Connect(int, sockaddr_in*)'
:talkserver.cc:(.text+0x6bc): undefined reference to `taskMgr::relationMap'
:talkserver.cc:(.text+0x72d): undefined reference to `taskMgr::ReceiveMsg(int)'
:talkserver.cc:(.text+0x7ee): undefined reference to `taskMgr::relationMap'
这是类的声明
class taskMgr
{
public:
static int Establish();
static int GetPortNum();
static void SetPortNum(int port);
static int GetAllConn();
static void HandleDisconnWhenSend(int id);
static map relationMap; //relations between sockets and taskMgr objects ids
static map taskMap; //ids and pointers that pointed to taskMgr
static bool islistening; //if islistening is false then the server quit
private:
static int coutAllConn; //all connections on the server
static int portNum; //listening port number
//maintains the count of taskMgr objects that ever exist,not current actually
static int idTaskMgr;
static int backlog;
public:
enum{TASK_IDLE, TASK_DIE, TASK_BLOCK}; //this used by m_status;
map m_userList; //
char m_recvBuff[MAXTRANSFERSIZE];
string m_sendStr;
private:
int m_status;
int m_conn; //connections that related to this taskMgr
int m_lastsocket; //represent the socket that is working with client
int m_id; //identifier of taskMgr object
public:
taskMgr();
~taskMgr();
bool operator==(const taskMgr mgr)const{return (m_id == mgr.m_id);}
int Connect(int socket, struct sockaddr_in* their_addr);
int ReceiveMsg(int socket);
int SendMsg(int socket, string msg);
void Disconnect(int socket);
int GetStatus();
void SetStatus(int status);
int GetConnections();
int GetLastSocket();
};
|
还有 static void HandleDisconnWhenSend(int id);
要改为如下:
void taskMgr::HandleDisconnWhenSend(int id)
另外:
static必须初始化。否则就会保错。
要改为如下:
void taskMgr::HandleDisconnWhenSend(int id)
另外:
static必须初始化。否则就会保错。
|
初始化代码:
bool taskMgr::islistening = true;
int taskMgr::backlog = 3;
int taskMgr::coutAllConn=0;
int taskMgr::portNum=0;
int taskMgr::idTaskMgr=0;
map taskMgr::relationMap;
map taskMgr::taskMap;
bool taskMgr::islistening = true;
int taskMgr::backlog = 3;
int taskMgr::coutAllConn=0;
int taskMgr::portNum=0;
int taskMgr::idTaskMgr=0;
map taskMgr::relationMap;
map taskMgr::taskMap;
|
那你的程序编译试了一下,注意原因你的代码中:
使用static 静态变量时,在你的.cpp中,要用如下格式:
taskMgr::静态变量
使用static 静态变量时,在你的.cpp中,要用如下格式:
taskMgr::静态变量