l********a 发帖数: 1154 | | A**u 发帖数: 2458 | | l********a 发帖数: 1154 | 3 就是个类自己包含自己的例子
例如,html的tag,
<...>
<...>
<...>
<...>
如果我用这个类A表示一个tag,每个tag下面可能有其他tag,都看做它的孩子,这样可以
对这些tag做相同的处理,如果没有孩子,它的children vector就是空的,现在需要写一
个析构函数来析构,由于vector存放的是指针形式A *,构造函数也能看出来每一个孩子
都是new出来的,调试的报错的确是在
void deallocate(pointer _Ptr, size_type)
{ // deallocate object at _Ptr, ignore size
::operator delete(_Ptr);
}
往上追踪看的确是这个类的析构函数出错了,不能delete?但是现在的析构函数从流程上
好像是对的,比较郁闷 | t****t 发帖数: 6806 | 4 A::~A() { for (auto i: children) delete i; }
【在 l********a 的大作中提到】 : 前几天问过一个类的初始化问题 : class A : { : public: : int num; : A *parent; : vector children; : ... : // 无参数调用 : A(void)
| b***i 发帖数: 3043 | 5 children->parent=a 是什么意思?
【在 l********a 的大作中提到】 : 前几天问过一个类的初始化问题 : class A : { : public: : int num; : A *parent; : vector children; : ... : // 无参数调用 : A(void)
| l********a 发帖数: 1154 | 6
就是如果构造函数传入第一个参数是当前构造对象的父指针
A::A(A* a,int)
{
this->parent = a;
...
}
【在 b***i 的大作中提到】 : children->parent=a 是什么意思?
| l********a 发帖数: 1154 | 7
auto会报错,
我写成类似这样的,还是会出错
for (int i=0;i<(int)(this->children_.size());++i)
{
A *temp = this->children_[i];
delete temp;
}
【在 t****t 的大作中提到】 : A::~A() { for (auto i: children) delete i; }
| b***i 发帖数: 3043 | 8 but Children is not A, it's vector
【在 l********a 的大作中提到】 : : auto会报错, : 我写成类似这样的,还是会出错 : for (int i=0;i<(int)(this->children_.size());++i) : { : A *temp = this->children_[i]; : delete temp; : }
| l********a 发帖数: 1154 | | t****t 发帖数: 6806 | 10 this is c++11, but you got the essence. in any case, you have other bug in
your code.
【在 l********a 的大作中提到】 : : children在this中是一个vector : 有参构造函数中是.push_back(new A(this,n))这种形式添加到this->children的 : 每一个child都是A *保存在这个vector中, : 就看做是html的tag吧, : : : : : ...
| | | l********a 发帖数: 1154 | 11
多谢,继续调试中.
【在 t****t 的大作中提到】 : this is c++11, but you got the essence. in any case, you have other bug in : your code.
| j*****g 发帖数: 16 | | t****t 发帖数: 6806 | 13 你这写的什么破烂?
【在 j*****g 的大作中提到】 : 摸黑写了一下,可是编译(g++ A.cpp)出错, 就是this->children->parent : A.cpp:20: error: base operand of ‘->’ has non-pointer type ‘std::vector: *, std::allocator >’ : A.cpp : //****************************** : 1 #include : 2 class A : 3 { : 4 public: : 5 int num;
| j*****g 发帖数: 16 | 14 简单测试一下楼主的class, 发现这句不能compile:
this->children->parent = a;
main.cpp: In constructor ‘A::A(A*, int)’:
main.cpp:25:
error: base operand of ‘->’ has non-pointer type ‘std::vector
allocator >’
这是为什么呢?多谢!
【在 t****t 的大作中提到】 : 你这写的什么破烂? : :
| t****t 发帖数: 6806 | 15 他显然只是东拼西凑了一小段给你看一下, 你只能看个意思, copy paste是不成的
【在 j*****g 的大作中提到】 : 简单测试一下楼主的class, 发现这句不能compile: : this->children->parent = a; : main.cpp: In constructor ‘A::A(A*, int)’: : main.cpp:25: : error: base operand of ‘->’ has non-pointer type ‘std::vector: allocator >’ : 这是为什么呢?多谢!
| d***q 发帖数: 1119 | | t****t 发帖数: 6806 | 17 你不用来来回回重复说明你的设计, 这个设计很简单, 大家都看明白了
大家问的是你混乱的语法问题, 你either没贴对or没贴全
【在 l********a 的大作中提到】 : : 多谢,继续调试中.
| l********a 发帖数: 1154 | 18 不好意思,原class是个非常大的类,为了说问题简单,
我手打的时候,把带参数的构造函数那句写错了,应该是
this->parent = a;
不是this->children->parent = a;
下面的测试用例复制可以编译运行的
#include
#include
using namespace std;
#include
class A
{
public:
int num;
A *parent;
vector children;
// 无参数调用
A(void)
{
this->num = 3;
this->parent = 0;
for (int i=0;inum;++i)
this->children.push_back(new A(this,this->num-1));
}
// 子构造函数,被上面的无参函数调用
A(A *a,int n)
{
this->num = n;
this->parent = a;
for (int i=0;inum;++i)
this->children.push_back(new A(this,this->num-1));
}
};
int main()
{
A aa;
cout << aa.children.size() << endl;
}
【在 j*****g 的大作中提到】 : 摸黑写了一下,可是编译(g++ A.cpp)出错, 就是this->children->parent : A.cpp:20: error: base operand of ‘->’ has non-pointer type ‘std::vector: *, std::allocator >’ : A.cpp : //****************************** : 1 #include : 2 class A : 3 { : 4 public: : 5 int num;
| b***i 发帖数: 3043 | 19 这就对了,你写好一个snippet,然后我们放到codepad.org上运行,就可以继续讨论了
另外,你那个A::~A里面的swap是谁跟谁?什么目的?
【在 l********a 的大作中提到】 : 不好意思,原class是个非常大的类,为了说问题简单, : 我手打的时候,把带参数的构造函数那句写错了,应该是 : this->parent = a; : 不是this->children->parent = a; : 下面的测试用例复制可以编译运行的 : #include : #include : using namespace std; : #include : class A
| t****t 发帖数: 6806 | 20 that is most probably copied from effective STL or some other book to "
really" release the vector memory. but it's actually not needed.
【在 b***i 的大作中提到】 : 这就对了,你写好一个snippet,然后我们放到codepad.org上运行,就可以继续讨论了 : 另外,你那个A::~A里面的swap是谁跟谁?什么目的?
| | | l********a 发帖数: 1154 | 21
这是从书上看的,说是vector退出code block并不会真正释放内存
需要跟空vector来swap交换,所以才加上去的
我看报错的位置,应该是deallocate的时候出错了,跟swap这句关系好像不大
我每次跟踪调试到那个delete _Ptr那句的时候,就没有上下文了,找不到到底哪里出的
错,但是vs ide的函数调用堆栈前面几个倒都是A **的析构,可能析构A *的时候编译器
需要A *的地址
【在 b***i 的大作中提到】 : 这就对了,你写好一个snippet,然后我们放到codepad.org上运行,就可以继续讨论了 : 另外,你那个A::~A里面的swap是谁跟谁?什么目的?
| t****t 发帖数: 6806 | 22 you read it wrong -- it said when you clear() the vector, or pop_back(), or
erase(), it probably won't release the memory.
but when you delete the vector, it will and it must.
read book carefully.
【在 l********a 的大作中提到】 : : 这是从书上看的,说是vector退出code block并不会真正释放内存 : 需要跟空vector来swap交换,所以才加上去的 : 我看报错的位置,应该是deallocate的时候出错了,跟swap这句关系好像不大 : 我每次跟踪调试到那个delete _Ptr那句的时候,就没有上下文了,找不到到底哪里出的 : 错,但是vs ide的函数调用堆栈前面几个倒都是A **的析构,可能析构A *的时候编译器 : 需要A *的地址
| b***i 发帖数: 3043 | 23 那你就作出一个能出错的snippet,放到codepad上出错,再贴出来代码,好吗?
另外,你怎么不用delete,而是直接~?
【在 l********a 的大作中提到】 : : 这是从书上看的,说是vector退出code block并不会真正释放内存 : 需要跟空vector来swap交换,所以才加上去的 : 我看报错的位置,应该是deallocate的时候出错了,跟swap这句关系好像不大 : 我每次跟踪调试到那个delete _Ptr那句的时候,就没有上下文了,找不到到底哪里出的 : 错,但是vs ide的函数调用堆栈前面几个倒都是A **的析构,可能析构A *的时候编译器 : 需要A *的地址
| l********a 发帖数: 1154 | 24
多谢,才知道这个网站
http://codepad.org/NLcZWxcd
我尝试了2种~A方法,调用析构函数的注释掉了
codepad上面有个Segmentation fault,应该跟析构有关
奇怪的是,我用code::blocks,gcc编译竟然没有错?2种析构都没报错
【在 b***i 的大作中提到】 : 那你就作出一个能出错的snippet,放到codepad上出错,再贴出来代码,好吗? : 另外,你怎么不用delete,而是直接~?
| t****t 发帖数: 6806 | 25 what is line 47 ?
【在 l********a 的大作中提到】 : : 多谢,才知道这个网站 : http://codepad.org/NLcZWxcd : 我尝试了2种~A方法,调用析构函数的注释掉了 : codepad上面有个Segmentation fault,应该跟析构有关 : 奇怪的是,我用code::blocks,gcc编译竟然没有错?2种析构都没报错
| b***i 发帖数: 3043 | 26 把显式调用~A去掉。
should I call destructor explicitly?
http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.5
【在 l********a 的大作中提到】 : : 多谢,才知道这个网站 : http://codepad.org/NLcZWxcd : 我尝试了2种~A方法,调用析构函数的注释掉了 : codepad上面有个Segmentation fault,应该跟析构有关 : 奇怪的是,我用code::blocks,gcc编译竟然没有错?2种析构都没报错
| l********a 发帖数: 1154 | 27 多谢2位,可能我代码哪里显式调用deconstructor了 | t****t 发帖数: 6806 | 28 ...跟你说话真够费劲儿的. 都已经给你指出行号了你还"可能".
【在 l********a 的大作中提到】 : 多谢2位,可能我代码哪里显式调用deconstructor了
| l********a 发帖数: 1154 | 29
哈哈,thrust兄别恼,我是说我实际的工程代码里面调用来调用去,哪个析构函数可能去
delete已经被deconstruct过的对象
不是说贴出来的例子
【在 t****t 的大作中提到】 : ...跟你说话真够费劲儿的. 都已经给你指出行号了你还"可能".
|
|