使用大小为8的未初始化值 [英] Use of uninitialised value of size 8
问题描述
我正在实现的fwd_iterator有一个非常奇怪的问题:
I'm having a quite strange problem with the fwd_iterator that I'm implementing:
如果我在类中定义的方法中使用迭代器,它们可以工作,但是如果我创建一个全局范围的方法,我使用迭代器,valgrind说我试图访问未初始化的内存。
if I use the iterators in methods defined inside the class, they work, but if I create a method with global scope in which I use iterators, valgrind says that I'm attempting to access to uninitialised memory.
似乎在类外创建的迭代器无法读取类的私有属性(即使使用公共方法创建这样做)。
It seems like iterators created outside the class cannot read a private attribute of the class (even with public methods created to do this).
这是全球范围的方法:
template<typename T, class Pred>
int evaluate(SparseMatrix<T> &sm, Pred pred){
typename SparseMatrix<T>:: iterator begin, end;
int count=0;
begin=sm.begin();
end=sm.end();
while(begin!=end){
if(pred(*(begin->data))) count++;
begin++;
}
int dcount=0;
if(pred(sm.getDef())) dcount = ((sm.getRows() * sm.getCols()) - sm.getSize());
return count+dcount;
}
这是内部类方法:
void print_it() {
iterator x=begin();
iterator y=end();
int i=1;
while(x!=y){
cout<<i<<".("<<(x->i)<<","<<(x->j)<<")="<<*(x->data)<<endl;
++x;
i++;
}
if(x==y) cout<<"End."<<endl;
cout<<endl;
}
解决:迭代器类有两个属性,value_type val和sm,指向类本身的指针。在operator =(const iterator& other)中,我忘了在val = other.val之后添加; sm = other.sm;行。
Solved: iterator class has two attributes, value_type val, and sm, a pointer to the class itself. In operator=(const iterator& other) I forgot to add after val=other.val; the sm=other.sm; line.
现在一切正常!
推荐答案
迭代器类有两个attributes,value_type val和sm,指向类本身的指针。在operator =(const iterator& other)中,我忘了在val = other.val之后添加; sm = other.sm; line。
iterator class has two attributes, value_type val, and sm, a pointer to the class itself. In operator=(const iterator& other) I forgot to add after val=other.val; the sm=other.sm; line.
现在一切正常!
这篇关于使用大小为8的未初始化值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!