c++问题 哪里错了????

zzyjsjcom 2007-08-01 08:47:20
#include <iostream>
using namespace std;

class B;
class test;

class B
{
public:
B(int ii)
{
cout << "B::B() " << endl;
i = ii;
}
void f() {cout << "B:f() i = " << i << endl;}
void f2()
{
t = new test(this);
delete t;
}
private:
int i;
test *t;

};

class test
{
public:
test() {}
test(B* b) {b.f();}
};

int main()
{
B b(5);
b.f2();
return 0;
}
...全文
180 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzyjsjcom 2007-08-02
  • 打赏
  • 举报
回复
不好意思,编译通过了 但为什么要把下面这段提出来呢? test类,我不是在前面声明过了吗?
squallchao 2007-08-02
  • 打赏
  • 举报
回复
是楼主环境的问题,我用g++编译通过,并得到了结果如下:
B::B()
B:f() i = 5

PS:编的是gexy()修改后的版本。
楼主把你遇到的错误贴出来看看
zzyjsjcom 2007-08-02
  • 打赏
  • 举报
回复
还是有错啊.........
gexy 2007-08-01
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

class B;
class test;

class B
{
public:
B(int ii)
{
cout << "B::B() " << endl;
i = ii;
}
void f() {cout << "B:f() i = " << i << endl;}
void f2();
private:
int i;
test *t;

};

class test
{
public:
test() {}
test(B* b) {b->f();} ///
};
void B::f2() //////////
{
t = new test(this);
delete t;
}

int main()
{
B b(5);
b.f2();
return 0;
}
Jack_xiao 2007-08-01
  • 打赏
  • 举报
回复
void f2()
{
t = new test(this); //这里有问题,this不确定
delete t;
}
class{
...
test(B* b) {b.f();} //指针应该用-〉吧
...
};

64,682

社区成员

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

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