请各位大神帮我看看这个指针到底哪里出错了?#include#includeusing namespace std;class point{public:point();point(int xx,int yy){x=xx;y=yy;}point();int getx(){return x;}private:int x,y;};int main(){point *p=new point;point a(2,3);p=&a

来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/20 08:48:17
请各位大神帮我看看这个指针到底哪里出错了?#include#includeusingnamespacestd;classpoint{public:point();point(intxx,intyy){

请各位大神帮我看看这个指针到底哪里出错了?#include#includeusing namespace std;class point{public:point();point(int xx,int yy){x=xx;y=yy;}point();int getx(){return x;}private:int x,y;};int main(){point *p=new point;point a(2,3);p=&a
请各位大神帮我看看这个指针到底哪里出错了?
#include
#include
using namespace std;
class point
{
public:
point();
point(int xx,int yy){x=xx;y=yy;}
point();
int getx(){return x;}
private:
int x,y;
};
int main()
{
point *p=new point;
point a(2,3);
p=&a;
cout

请各位大神帮我看看这个指针到底哪里出错了?#include#includeusing namespace std;class point{public:point();point(int xx,int yy){x=xx;y=yy;}point();int getx(){return x;}private:int x,y;};int main(){point *p=new point;point a(2,3);p=&a
这个代码错了两个地方,LZ只改了一处:
1)构造函数和析构函数都只有声明,没有定义,同时使用构造函数初始化错误;
2)p重新指向了a的地址,a的地址是在栈不能delete,需要把delete提前:
#include
#include
using namespace std;
class point
{
public:
point(){} //这里
point(int xx,int yy){x=xx;y=yy;}
~point(){} //这里
int getx(){return x;}
private:
int x,y;
};
int main()
{
point *p=new point(); //这里
point a(2,3);
delete p; //还有这里
p=&a;
cout