pure specifier can only be specified for functions

来源:互联网 发布:js数组slice 编辑:程序博客网 时间:2024/06/09 18:47

源码:版本vc6.0

class CTest{private:int index = 0;char* name = NULL;public:void setname(char* str);};void CTest::setname(char* str){name = str;printf("%s",name);}int main(int argc, char* argv[]){CTest tt;char* name = "Hello,kitty!\n";tt.setname(name);return 0;}


编译后提示:

--------------------Configuration: Demo_1 - Win32 Debug--------------------Compiling...Demo_1.cppE:\SAVES\VC\Start\Demo_1\Demo_1.cpp(12) : error C2252: 'index' : pure specifier can only be specified for functionsE:\SAVES\VC\Start\Demo_1\Demo_1.cpp(13) : error C2252: 'name' : pure specifier can only be specified for functionsE:\SAVES\VC\Start\Demo_1\Demo_1.cpp(18) : error C2065: 'name' : undeclared identifierE:\SAVES\VC\Start\Demo_1\Demo_1.cpp(18) : error C2440: '=' : cannot convert from 'char *' to 'int'        This conversion requires a reinterpret_cast, a C-style cast or function-style castError executing cl.exe.Demo_1.exe - 4 error(s), 0 warning(s)


这里出问题的原因是:成员变量index和name在声明时被初始化了,将index=0和name=NULL的初始化去掉。即:

class CTest{private:int index;char* name;public:void setname(char* str);};


 

经Baidu还有一个可能的原因是静态成员变量的原因,具体见http://blog.csdn.net/zxxyyxf/article/details/6432612