静态数据成员有关问题:搞了个小程序,就是看不出有什么东东,麻烦大家给看看
静态数据成员问题:搞了个小程序,就是看不出有什么错误,麻烦大家给看看。
程序是这样的:
//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)
程序是这样的,可编译时出现问题,我怎么也看不出是哪里有问题,清高手指点!!!谢谢!!
------解决方案--------------------
//point.cpp
#include "point.h "
point::point(int xx,int yy)
{cout < < "构造函数被调用! " < <endl;
x=xx;
y=yy;
countp++;//这里的分号有问题!!自己看看就知道了哦...
}
------解决方案--------------------
呵呵 getc和系统函数冲突?
------解决方案--------------------
point::point(point &p1)
{x=p1.x;
y=p1.y;
countp++;
}
构造函数好象不能拿对象当参数的吧,还有用对象给类的数据成员符值好象也是不行的吧~逻辑上有点不通
------解决方案--------------------
http://www.pconline.com.cn/pcedu/empolder/gj/c/0503/571606.html
程序是这样的:
//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)
程序是这样的,可编译时出现问题,我怎么也看不出是哪里有问题,清高手指点!!!谢谢!!
------解决方案--------------------
//point.cpp
#include "point.h "
point::point(int xx,int yy)
{cout < < "构造函数被调用! " < <endl;
x=xx;
y=yy;
countp++;//这里的分号有问题!!自己看看就知道了哦...
}
------解决方案--------------------
呵呵 getc和系统函数冲突?
------解决方案--------------------
point::point(point &p1)
{x=p1.x;
y=p1.y;
countp++;
}
构造函数好象不能拿对象当参数的吧,还有用对象给类的数据成员符值好象也是不行的吧~逻辑上有点不通
------解决方案--------------------
http://www.pconline.com.cn/pcedu/empolder/gj/c/0503/571606.html