宏定义如下:
#define NS_CC_EXT_BEGIN namespace cocos2d { namespace extension {
#define NS_CC_EXT_END }}
头文件如下:
#pragma once
#include <pthread\pthread.h>
#include "cocoa\CCArray.h"
#include "ExtensionMacros.h"
NS_CC_EXT_BEGIN
class NetworkCommunication
{
private:
static NetworkCommunication * s_instance;
static CCArray * s_requestQueue;
static CCArray * s_responseQueue;
static pthread_t s_networkReadThread;
static pthread_t s_networkWriteThread;
static pthread_mutex_t s_requestQueueMutex;
static pthread_mutex_t s_responseQueueMutex;
static pthread_mutex_t s_readThreadSleepMutex;
static pthread_cond_t s_readThreadSleepCondition;
static pthread_mutex_t s_writeThreadSleepMutex;
static pthread_cond_t s_writeThreadSleepCondition;
public:
static NetworkCommunication * getInstance();
void start();
private:
NetworkCommunication(void);
~NetworkCommunication(void);
};
NS_CC_EXT_END
cpp文件内容如下:
#include "NetworkCommunication.h"
#include <pthread\pthread.h>
NS_CC_EXT_BEGIN
static void* networkReadThreadWork(void *data) {
CCLog("Jonathan: in networkReadThreadWork");
while (true) {
}
return NULL;
}
static void* networkWriteThreadWork(void *data) {
CCLog("Jonathan: in networkWriteThreadWork");
return NULL;
}
NetworkCommunication::NetworkCommunication(void)
{
s_requestQueue = new CCArray();
s_requestQueue->init();
s_responseQueue = new CCArray();
s_responseQueue->init();
pthread_mutex_init(&s_requestQueueMutex, NULL);
pthread_mutex_init(&s_responseQueueMutex, NULL);
pthread_mutex_init(&s_readThreadSleepMutex, NULL);
pthread_cond_init(&s_readThreadSleepCondition, NULL);
pthread_mutex_init(&s_writeThreadSleepMutex, NULL);
pthread_cond_init(&s_writeThreadSleepCondition, NULL);
pthread_create(&s_networkReadThread, NULL, networkReadThreadWork, NULL);
pthread_detach(s_networkReadThread);
pthread_create(&s_networkWriteThread, NULL, networkWriteThreadWork, NULL);
pthread_detach(s_networkWriteThread);
}
NetworkCommunication::~NetworkCommunication(void)
{
}
NetworkCommunication * NetworkCommunication::getInstance() {
if (s_instance == NULL) {
s_instance = new NetworkCommunication();
}
return s_instance;
}
NS_CC_EXT_END
按F7构建时,发生如下的错误:
3>NetworkCommunication.obj : error LNK2001: 无法解析的外部符号 "private: static class cocos2d::extension::NetworkCommunication * cocos2d::extension::NetworkCommunication::s_instance" (?s_instance@NetworkCommunication@extension@cocos2d@@0PAV123@A)
3>NetworkCommunication.obj : error LNK2001: 无法解析的外部符号 "private: static class cocos2d::CCArray * cocos2d::extension::NetworkCommunication::s_requestQueue" (?s_requestQueue@NetworkCommunication@extension@cocos2d@@0PAVCCArray@3@A)
3>NetworkCommunication.obj : error LNK2001: 无法解析的外部符号 "private: static class cocos2d::CCArray * cocos2d::extension::NetworkCommunication::s_responseQueue" (?s_responseQueue@NetworkCommunication@extension@cocos2d@@0PAVCCArray@3@A)
3>NetworkCommunication.obj : error LNK2001: 无法解析的外部符号 "private: static struct ptw32_handle_t cocos2d::extension::NetworkCommunication::s_networkReadThread" (?s_networkReadThread@NetworkCommunication@extension@cocos2d@@0Uptw32_handle_t@@A)
3>NetworkCommunication.obj : error LNK2001: 无法解析的外部符号 "private: static struct ptw32_handle_t cocos2d::extension::NetworkCommunication::s_networkWriteThread" (?s_networkWriteThread@NetworkCommunication@extension@cocos2d@@0Uptw32_handle_t@@A)
3>NetworkCommunication.obj : error LNK2001: 无法解析的外部符号 "private: static struct pthread_mutex_t_ * cocos2d::extension::NetworkCommunication::s_requestQueueMutex" (?s_requestQueueMutex@NetworkCommunication@extension@cocos2d@@0PAUpthread_mutex_t_@@A)
3>NetworkCommunication.obj : error LNK2001: 无法解析的外部符号 "private: static struct pthread_mutex_t_ * cocos2d::extension::NetworkCommunication::s_responseQueueMutex" (?s_responseQueueMutex@NetworkCommunication@extension@cocos2d@@0PAUpthread_mutex_t_@@A)
3>NetworkCommunication.obj : error LNK2001: 无法解析的外部符号 "private: static struct pthread_mutex_t_ * cocos2d::extension::NetworkCommunication::s_readThreadSleepMutex" (?s_readThreadSleepMutex@NetworkCommunication@extension@cocos2d@@0PAUpthread_mutex_t_@@A)
3>NetworkCommunication.obj : error LNK2001: 无法解析的外部符号 "private: static struct pthread_cond_t_ * cocos2d::extension::NetworkCommunication::s_readThreadSleepCondition" (?s_readThreadSleepCondition@NetworkCommunication@extension@cocos2d@@0PAUpthread_cond_t_@@A)
3>NetworkCommunication.obj : error LNK2001: 无法解析的外部符号 "private: static struct pthread_mutex_t_ * cocos2d::extension::NetworkCommunication::s_writeThreadSleepMutex" (?s_writeThreadSleepMutex@NetworkCommunication@extension@cocos2d@@0PAUpthread_mutex_t_@@A)
3>NetworkCommunication.obj : error LNK2001: 无法解析的外部符号 "private: static struct pthread_cond_t_ * cocos2d::extension::NetworkCommunication::s_writeThreadSleepCondition" (?s_writeThreadSleepCondition@NetworkCommunication@extension@cocos2d@@0PAUpthread_cond_t_@@A)
3>E:\cocos2d-x-2.2.3\projects\Example\proj.win32\Debug.win32\Example.exe : fatal error LNK1120: 11 个无法解析的外部命令
类的命名空间指定了之后,类静态成员的命名空间需要单独说明吗?先在这里mark一下这个问题,等后面有时间再研究这个问题。