类私有静态成员编译不能通过。
文件:
#ifndef ANDROID_MYCLIENT_H
#define ANDROID_MYCLIENT_H
#include "imyclient.h"
namespace android
{
class MyClient:public BnMyClient, public IBinder::DeathRecipient {
public:
~MyClient();
void add100(int n);
void getnum(int a, int b);
void getstring(const char *str);
void sendbinder();
virtual void sendint(int i);
private:
MyClient();
virtual void binderDied(const wp<IBinder>& who);
class DeathNotifier:public IBinder::DeathRecipient
{
public:
DeathNotifier(){}
virtual void binderDied(const wp<IBinder>& who);
};
friend class DeathNotifier;
static sp<DeathNotifier> mDeathNotfier;
static const void getMyService();
//通过ServiceManager获取服务接口
};
}; //namespace
#endif
cpp文件
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
#include "myclient.h"
namespace android
{
sp<MyClient::DeathNotifier> MyClient::mDeathNotifier;
sp<IBinder> binder;
//sp<MyClient::DeathNotifier> MyClient::mDeathNotifier;
MyClient::MyClient()
{
}
MyClient::~MyClient()
{
}
void MyClient::add100(int n)
{
getMyService();
Parcel data, reply;
int answer;
data.writeInt32(getpid());
data.writeInt32(n);
LOGE("BpMyService::create remote()->transact()/n");
binder->transact(0, data, &reply);
answer = reply.readInt32();
printf("answner=%d\n", answer);
return;
}
void MyClient::getnum(int a, int b)
{
getMyService();
Parcel data,reply;
int answer;
data.writeInt32(getpid());
data.writeInt32(a);
data.writeInt32(b);
binder->transact(1, data, &reply);
answer = reply.readInt32();
printf("1+2 = %d\n",answer);
}
void MyClient::getstring(const char *str)
{
getMyService();
Parcel data, reply;
LOGE("SendMsg: %s", str);
data.writeInt32(getpid());
data.writeCString(str);
binder->transact(2, data, &reply);
const char *pAnswer=reply.readCString();
printf("pAnswer = %s\n", pAnswer);
}
void MyClient::sendbinder()
{
getMyService();
Parcel data, reply;
data.writeStrongBinder(this->asBinder());
binder->transact(3, data, &reply);
}
void MyClient::sendint(int i)
{
printf("myclient sendint");
}
const void MyClient::getMyService()
{
sp<IServiceManager> sm = defaultServiceManager();
binder = sm->getService(String16("android.myservice"));
LOGE("MyClient::getMyService %p/n",sm.get());
if (binder == 0) {
LOGW("MyService not published, waiting...");
return;
}
if (mDeathNotifier == NULL)
mDeathNotifier = new DeathNotifier();
binder->linkToDeath(mDeathNotifier);
}
void MyClient::binderDied(const wp<IBinder>& who)
{
LOGW("MyClient die");
}
void MyClient::DeathNotifier::binderDied(const wp<IBinder> &who)
{
LOGW("MyServer die");
}
}; //namespace
make:进入目录'/opt/android4.0'
target thumb C++: libmyclient <= frameworks/base/myservice/libmyclient/imyclient.cpp
target thumb C++: libmyclient <= frameworks/base/myservice/libmyclient/myclient.cpp
In file included from frameworks/base/myservice/libmyclient/myclient.cpp:5:
frameworks/base/myservice/libmyclient/myclient.h:31: warning: type qualifiers ignored on function return type
frameworks/base/myservice/libmyclient/myclient.cpp:12: error: 'android::sp<android::MyClient::DeathNotifier> android::MyClient::mDeathNotifier' is not a static member of 'class android::MyClient'
frameworks/base/myservice/libmyclient/myclient.cpp:91: warning: type qualifiers ignored on function return type
frameworks/base/myservice/libmyclient/myclient.cpp: In static member function 'static const void android::MyClient::getMyService()':
frameworks/base/myservice/libmyclient/myclient.cpp:108: error: 'mDeathNotifier' was not declared in this scope
frameworks/base/myservice/libmyclient/myclient.cpp:110: error: 'mDeathNotifier' was not declared in this scope
make: *** [out/target/product/generic/obj/SHARED_LIBRARIES/libmyclient_intermediates/myclient.o] 错误 1
make:离开目录“/opt/android4.0”
我另外定义一个私有成员变量:
private:
static int aaa;
就没有这个问题。
大家帮忙看看,怎么回事。
[解决办法]
楼主忘记怎么定义静态成员变量了。在类内部那是个申明。需要在类的外部进行定义。
比如:
class A{
static int a;
};
int A::a = 10;
[解决办法]
你没有初始化。静态成员变量的初始化方法相对比较特殊。各种特殊成员变量的初始化方法,可以参考:
特殊数据类型成员变量的初始化
