一个简单的C++语法问题。

richoboy 2008-10-09 10:43:40

#include <iostream.h>

class A
{
public:
A()
{
cout << "this is A" << endl;
}
~A(){}

void outputA()
{
cout << "outputA" << x << y << endl;
}
int x;
int y;
};

class B
{
public:
B()
{
cout << "this is B" << endl;
pA = NULL;
}
~B(){}

void outputB(int m,int n)
{
cout << "outputB" << endl;
pA->x=m;
pA->y=n;
pA->outputA();
}
A *pA;
};

int main()
{

B cB;
cB.outputB(4,3);
cout << cB.pA->x << endl << cB.pA->y << endl;
return 0;
}


这个代码在哪里出现了问题?怎么解决。谢谢了
...全文
94 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
handsomezwm 2008-10-09
  • 打赏
  • 举报
回复

#include <iostream.h>

class A
{
public:
A()
{
cout << "this is A" << endl;
}
~A(){}

void outputA()
{
cout << "outputA" << x << y << endl;
}
int x;
int y;
};

class B
{
public:
B()
{
cout << "this is B" << endl;
pA = new A(); //问题出现在这个地方,程序中PA是一个空指针,所以你不能为其赋值,而给他一个
} //分配一个指向的空间就OK了
~B(){}

void outputB(int m,int n)
{
cout << "outputB" << endl;
pA->x=m;
pA->y=n;
pA->outputA();
}
A *pA;
};

int main()
{

B cB;
cB.outputB(4,3);
cout << cB.pA->x << endl << cB.pA->y << endl;
return 0;
}
zhouqiang714 2008-10-09
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

class A
{
public:
A()
{
cout << "this is A"<<endl;
}
~A(){}

void outputA()
{
cout << "outputA" << x << y << endl;
}
int x;
int y;
};

class B :public A
{

public:
B()
{
cout<< "this is B"<< endl;
}
~B(){}
void outputB(int m,int n)
{
cout<< "outputB"<< endl;
pA.x=m;
pA.y=n;
pA.outputA();
}
A pA;
};

int main()
{

B cB;
cB.outputB(4,3);
cout<< cB.pA.x<< endl<< cB.pA.y<< endl;
system("pause");
return 0;
}
sk19891117 2008-10-09
  • 打赏
  • 举报
回复
void outputB(int m,int n)
{
cout < < "outputB" < < endl;
pA->x=m;
pA->y=n;
pA->outputA();
}
A *pA;



把A *pA放前面会不会好点呢?(不成熟的意见)
  • 打赏
  • 举报
回复
你的pA没有指向实际对象阿,一直是null,所以会出错阿
zclever 2008-10-09
  • 打赏
  • 举报
回复

#include <iostream.h>

class A
{
public:
A()
{
cout << "this is A" << endl;
}
~A(){}

void outputA()
{
cout << "outputA" << x << y << endl;
}
int x;
int y;
};

class B
{
public:
B()
{
cout << "this is B" << endl;
//pA = NULL;
pA=new A; //记得给A分配空间
}
~B(){delete pA;} //删除空间

void outputB(int m,int n)
{
cout << "outputB" << endl;
pA->x=m;
pA->y=n;
pA->outputA();
}
A *pA;
};

int main()
{

B cB;
cB.outputB(4,3);
cout << cB.pA->x << endl << cB.pA->y << endl;
return 0;
}

richoboy 2008-10-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cymvp 的回复:]
class B
{
public:
B()
{
cout < < "this is B" < < endl;
pA = NULL;
}
~B(){}
中的pA是什么类型?当然会出错了 !
[/Quote]
这个是给pA初始化啊,让他指向空。
richoboy 2008-10-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 richoboy 的回复:]
C/C++ code#include<iostream.h>classA
{public:
A()
{
cout<<"this is A"<<endl;
}~A(){}voidoutputA()
{
cout<<"outputA"<<x<<y<<endl;
}intx;inty;
};classB
{public:
B()
{
cout<<"this is B"<<endl;
pA=NULL;
}~B(){}voidoutputB(intm,intn)
{
cout<<"outputB"<<endl;
pA->x=m;
pA->y=n;
pA->outputA();
}
A*pA;
};intmain(…
[/Quote]

为什么不是啊,我在A中定义了x,y二个成员啊.
lionc650 2008-10-09
  • 打赏
  • 举报
回复
晕,看错了。。。lz这排版太难看了....
  • 打赏
  • 举报
回复
class B
{
public:
B()
{
cout < < "this is B" < < endl;
pA = NULL;
}
~B(){}
中的pA是什么类型?当然会出错了 !
lionc650 2008-10-09
  • 打赏
  • 举报
回复

void outputA()
{
cout < < "outputA" < < x < < y < < endl;
}
int x;
int y;
};

void outputB(int m,int n)
{
cout < < "outputB" < < endl;
pA->x=m;
pA->y=n;
pA->outputA();
}
A *pA;
};

这都什么跟什么啊...... x,y可不是A的成员。 建议楼主看看面向对象的基础..
richoboy 2008-10-09
  • 打赏
  • 举报
回复
#include <iostream.h>

class A
{
public:
A()
{
cout < < "this is A" < < endl;
}
~A(){}

void outputA()
{
cout < < "outputA" < < x < < y < < endl;
}
int x;
int y;
};

class B
{
public:
B()
{
cout < < "this is B" < < endl;
pA = NULL;
}
~B(){}

void outputB(int m,int n)
{
cout < < "outputB" < < endl;
pA->x=m;
pA->y=n;
pA->outputA();
}
A *pA;
};

int main()
{

B cB;
cB.outputB(4,3);
cout < < cB.pA->x < < endl < < cB.pA->y < < endl;
return 0;
}

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧