我正在使用NTL C ++库。在尝试执行以下代码时:
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;
}
问题似乎源于set(* one)代码行。
我一直试图了解代码中出了什么问题,但无济于事。任何帮助表示赞赏。
答案 0 :(得分:0)
GF2的头文件也声明了类
ref_GF2
用于表示非常量引用到GF2
,[...]。从
ref_GF2
到const GF2
的隐式转化 从GF2&
到ref_GF2
。
您收到错误是因为您正在定义没有目标的引用。
在您致电set(*one)
时,*one
未指向GF2
,因此会引发错误。
如果您在致电GF2
之前指向set(*one)
:
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
。