#include <iostream.h>
#include <string.h>
class Person{
public:
char m_strName[20];
long m_ID;
public:
Person(char*strName,long ID)
{strcpy (m_strName,strName);m_ID=ID;}
static long GetID()
{return m_ID;}
};
void main()
{
Person person1(“LiuJun”,1101640524);
cout <<“ID=”<<Person::GetID()<<“\n”;
}
error C2597: illegal reference to data member “Person::m_ID” in a static member function
Error executing cl.exe.
#include <string.h>
class Person{
public:
char m_strName[20];
long m_ID;
public:
Person(char*strName,long ID)
{strcpy (m_strName,strName);m_ID=ID;}
static long GetID()
{return m_ID;}
};
void main()
{
Person person1(“LiuJun”,1101640524);
cout <<“ID=”<<Person::GetID()<<“\n”;
}
error C2597: illegal reference to data member “Person::m_ID” in a static member function
Error executing cl.exe.
解决方案
15
static long GetID()
{return m_ID;}
要对应:
static long m_ID;
不过你的题意不是这样,所以都去掉static吧。
{return m_ID;}
要对应:
static long m_ID;
不过你的题意不是这样,所以都去掉static吧。
50
静态成员函数不能返回非静态成员变量
static long GetID()
改成
long GetID()
static long GetID()
改成
long GetID()