注册 登录
编程论坛 C++教室

c++静态数据成员编译问题,看不出错误在哪,请高手指点!!

cyliqiang 发布于 2007-04-08 10:07, 1360 次点击

程序是这样的:


//point.h
#include <iostream>
using namespace std;
class point
{
public:
point(int xx,int yy);
point(point &p1);
int getx();
int gety();
static int getc();
virtual ~point();

private:
int x;
int y;
static int countp;

};


//point.cpp
#include "point.h"
point::point(int xx,int yy)
{cout<<"构造函数被调用!"<<endl;
x=xx;
y=yy;
countp++;
}

point::~point()
{ cout<<"析构函数被调用!"<<endl;
countp--;

}
int point::getc()
{return countp;}

int point::getx()
{return x;}
int point::gety()
{return y;}

point::point(point &p1)
{x=p1.x;
y=p1.y;
countp++;
}

int point::countp=0;


//main.cpp
#include "point.h"
int main()
{

point p1(1,5);
point p2(2,5);
cout<<"p1点的x坐标是:"<<endl;
cout<<p1.getx()<<endl;
cout<<"p1点的y坐标是:"<<endl;
cout<<p1.gety()<<endl;
cout<<"p2点的x坐标是:"<<endl;
cout<<p2.getx()<<endl;
cout<<"p2点的y坐标是:"<<endl;
cout<<p2.gety()<<endl;
cout<<"此时共有的个数是:"<<endl;
cout<<p1.getc()<<endl;
return 0;
}
编译结果:
warning C4003: not enough actual parameters for macro 'getc'
h:\c++学习\随便练习\静态数据成员\point.h(20) : error C2059: syntax error : '--'
h:\c++学习\随便练习\静态数据成员\point.h(20) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
h:\c++学习\随便练习\静态数据成员\main.cpp(16) : warning C4003: not enough actual parameters for macro 'getc'
h:\c++学习\随便练习\静态数据成员\main.cpp(23) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

main.obj - 3 error(s), 2 warning(s)


程序是这样的,可编译时出现问题,我怎么也看不出是哪里有问题,清高手指点!!!谢谢!!


4 回复
#2
天下第二刀2007-04-08 10:40
我晕, getc 这是系统定义了的函数呀, 换个名字
#3
ddsxd2007-04-08 11:27
回复:(cyliqiang)c++静态数据成员编译问题,看不出...
我碰到过类似问题,类中的静态数据成员需要在类外分配空间和初始化。
你在头文件的最后加一句:int point::countp=0;
试试看吧,我也拿不准

[此贴子已经被作者于2007-4-8 11:28:54编辑过]

#4
cyliqiang2007-04-08 11:31
谢谢了,问题解决了,就是函数成员名字错了。哈哈
#5
wanjiang2008-11-12 16:59
换个名字就可以了,那个getc就是系统定义过的了函数,换个名字就可以编译通过了
1