用静态变量统计C++对象个数的问题,求大侠帮忙
狼痕 2011-11-25 10:56:30 下面是源代码?这个程序编译器老是说没有定义nCount,为什么呀?明明已经定义啦?
#include<iostream>
#include"head.h"
using namespace std;
class CPoint
{
private:
int x,y;
static int nCount; // nCount用于保存点的个数
public:
CPoint(int px=0, int py=0);
CPoint(CPoint&);
~CPoint();
int GetX();
int GetY();
void SetX(int);
void SetY(int);
void ShowPoint();
};
CPoint::CPoint(int px,int py)
{
SetX(px);
SetY(py);
nCount++;
}
CPoint::CPoint(CPoint& p)//拷贝构造函数
{
SetX(p.GetX());
SetY(p.GetY());
nCount++;
}
CPoint::~CPoint()
{
nCount--;
}
int CPoint::GetX()
{
return x;
}
int CPoint::GetY()
{
return y;
}
void CPoint::SetX(int a)
{
x = a;
}
void CPoint::SetY(int b)
{
y = b;
}
void CPoint::ShowPoint()
{
cout << "(" << x << "," << y << ")" << endl;
}