我有一个由我制作的 ArbDouble 类,它基本上是mpfr_t的包装器
这个问题的类的相关部分是
class ArbDouble
{
private:
mpfr_t value;
public:
explicit ArbDouble(long double value_, unsigned long precision) {
mpfr_init2(value, precision);
mpfr_set_d(value, value_, MPFR_RNDN);
}
explicit ArbDouble(long int value_, unsigned long precision) {
mpfr_init2(value, precision);
mpfr_set_ui(value, value_, MPFR_RNDN);
}
ArbDouble(){
mpfr_init(value);
}
ArbDouble(unsigned long precision) {
mpfr_init2(value,precision);
}
ArbDouble(const ArbDouble& other) {
mpfr_init2(value, other.getPrecision());
mpfr_set(value, other.value, MPFR_RNDN);
}
ArbDouble(const mpfr_t& other) {
mpfr_init2(value, mpfr_get_prec(other));
mpfr_set(value, other, MPFR_RNDN);
}
explicit ArbDouble(char* other, unsigned long precision) {
mpfr_init2(value, precision);
mpfr_set_str(value, other, 10, MPFR_RNDN);
}
~ArbDouble() {
mpfr_clear(value);
}
inline unsigned long getPrecision() const
{
return mpfr_get_prec(value);
}
inline ArbDouble& operator=(const ArbDouble &other)
{
mpfr_set_prec(value, other.getPrecision());
mpfr_set(value, other.value, MPFR_RNDN);
return *this;
}
}
现在,我使用 std::vector 来存储这些值的矩阵,例如
std::vector<ArbDouble> temp;
temp.push_back(ArbDouble((long int)0,64)); // calling "ArbDouble(long int value_, unsigned long precision)"
std::vector<std::vector<ArbDouble> > currentOrbit;
currentOrbit.push_back(temp);
这会导致 linux 机器上的段错误,但不会在 mac 机器上。
gdb给出的错误是:
Program received signal SIGSEGV, Segmentation fault.
0x00000000004069fe in std::vector<std::vector<ArbDouble, std::allocator<ArbDouble> >, std::allocator<std::vector<ArbDouble, std::allocator<ArbDouble> > > >::push_back (this=0xb5daafcc938b13f6, __x=std::vector of length 1, capacity 1 = {...})
at /usr/include/c++/4.5/bits/stl_vector.h:743
743 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
有谁知道问题出在哪里?我不是一个很好的类设计师,所以,我的猜测是它的某个地方有缺陷。任何建议都非常欢迎!!!