2016-02-27 54 views
1

我正在使用NTL C++库。在尝试执行以下代码:NTL库ref_GF2运行时错误

NTL::ref_GF2 *zero = new NTL::ref_GF2(); 
NTL::ref_GF2 *one = new NTL::ref_GF2(); 
set(*one); 

我正在一个EXC_BAD_INSTRUCTION错误:

ref_GF2 operator=(long a) 
{ 
    unsigned long rval = a & 1; 
    unsigned long lval = *_ref_GF2__ptr; 
    lval = (lval & ~(1UL << _ref_GF2__pos)) | (rval << _ref_GF2__pos); 
    *_ref_GF2__ptr = lval; 
    return *this; 
} 

这个问题似乎从集合(*一个)的代码行干。

我一直在试图理解代码中出了什么问题,但没有用。任何帮助赞赏。

回答

0

documentation

The header file for GF2 also declares the class ref_GF2 , which use used to represent non-const references to GF2 's, [...].

There are implicit conversions from ref_GF2 to const GF2 and from GF2& to ref_GF2 .

你,因为你的定义是没有目标的参照得到错误。 在您拨打set(*one)时,*one未指向GF2,因此会引发错误。

它工作正常,如果你调用set(*one)前指向一个GF2

NTL::GF2 x = GF2(); 
NTL::set(x);    // x = 1 

NTL::ref_GF2 *zero = new NTL::ref_GF2(x); 
NTL::ref_GF2 *one = new NTL::ref_GF2(x); 

// this works now 
NTL::clear(*zero); 
NTL::set(*one); 

cout << *zero << endl;  // prints "1" 
cout << *one << endl;  // prints "1" 

注意ref_GF2表示对GF2参考。我的示例代码显示零和一个都指向x。也许你想用GF2而不是ref_GF2

+0

嘿,刚刚解决了这个问题。你是对的,我不得不使用GF2而不是ref_GF2。感谢您的反馈! –