Segfault返回一个元组

时间:2016-08-20 16:14:37

标签: c++ struct segmentation-fault tuples tie

我一直试图用2个自定义类Fraction和Integer重载+运算符。理想情况下,我希望+运算符返回最简单的操作版本(即1/4 + 3/4 == 1(整数))。我还没有找到一种动态分配返回类型的好方法,所以我试图返回包含在Struct或元组中的多个值。当我在main中实际尝试操作时,我会修改一个段错误,如下所示:

///main/////////
int main(){
  Fraction *f = new Fraction(1,4);
  Fraction *f2 = new Fraction(3,4);
  Fraction *resF = new Fraction();//results
  Integer *resI = new Integer();

  boost::tie(resF, resI) = *f+*f2; //SEGFAULT here
}

涉及的两个类是公共抽象基类的派生,其中定义了成员和函数:

#include <boost/tuple/tuple.hpp>
#include <iostream>
//Number class
//forward declarations for tuple
class Integer;
class Fraction;
//abstract base class
template<class T>//T is derived class
class Number{
  virtual const boost::tuple<Fraction*, Integer*> operator+ (const Number&) {};
  virtual void display(std::ostream &) const {} ;
  virtual bool operator==(const Number& rhs) const{} ;
};//end of Number class

//Integer class
class Integer: public Number<Integer>{
  int numericValue;//<! the value of the integer
  public:
  int getValue() const;//<!access private member variable numericValue
  void setValue(int);//<!set private member variable numericValue
  Integer();//<!default constructor
  Integer(int);//<!param constructor
  virtual ~Integer() {}//<!destructor
  //display
  void display(std::ostream &) const;//<!stream a display of the number
  //int == int
  bool operator==(const Integer&) const;//<! comparator int-int
  //  int + int
  const Integer operator+ (const Integer &);//<! add int+int
}; 
//DEFINITIONS////////////////////
//Default constructor
Integer::Integer(){
  numericValue = 0;
}
// param constructor
Integer::Integer(int num){
  numericValue = num;
}
//get integer value
int Integer::getValue() const{
  return this->numericValue;
}
//set integer value
void Integer::setValue(int x){
  this->numericValue = x;
}
//display int 
void Integer::display(std::ostream& stream) const{
  stream << this->numericValue<<std::endl;
}
// int + int
const Integer Integer::operator+(const Integer &rhs){
  Integer  temp = this->numericValue + rhs.numericValue;
  return temp;
}
// int == int
bool Integer::operator==(const Integer& rhs) const{
  if(this->numericValue == rhs.numericValue)
    return true;
  else
    return false;
}
//end of Integer class

//Fraction class
class Fraction: public Number<Fraction>{
  Integer numerator;
  Integer denominator;

  boost::tuple<Fraction*, Integer*> resOfAdd;

  public:
  int getNumerator();//<! to access private member
  int getDenominator();//<! to access private member
  bool isInteger;//<! flag if the fraction result of '+' can be reduced as an integer 
  bool isWhole();//!<tells if can be simplified to integer
  Integer fToI;//<! store the integer value of the fraction if it is whole 
  Fraction() = default;//<! default constructor
  Fraction(const int &, const int &);//<!param constructor
  const Fraction simplify(const Fraction &in);//<! simplifies fraction if possible
  int gcdCalculate(int  lhs, int  rhs);//!<greatest common denominator
  int lcmCalculate(const int  lhs, const int  rhs);//<!least common 
  virtual ~Fraction() {}
  //display
  void display(std::ostream &) const;
  // frac == frac 
  bool operator==(const Fraction& rhs) const;
  //frac + frac
  boost::tuple<Fraction*, Integer*>  operator+(const Fraction &);
};//end of Fraction class
//DEFINITIONS///////////////////
// param constructor 
Fraction::Fraction(const int & num, const int & den){
  numerator.setValue(num);
  denominator.setValue(den);
  if(denominator.getValue()==1){//also an integer
    fToI = Integer(numerator.getValue());
  }
  if(denominator.getValue() < 0 && numerator.getValue() > 0){//negative sign on bottom
    denominator.setValue(denominator.getValue()*-1);
    numerator.setValue(numerator.getValue()*-1); //switch it to the top
  }
  if(denominator.getValue() < 0 && numerator.getValue() < 0){//both top and bottom are negative
    denominator.setValue(denominator.getValue()*-1);
    numerator.setValue(numerator.getValue()*-1); //flip them to positive
  }
}
//get ifInteger
bool Fraction::isWhole(){
  return this->isInteger;
}
//get numerator
int Fraction::getNumerator(){
  return this->numerator.getValue();
}
//get denominator
int Fraction::getDenominator(){
  return this->denominator.getValue();
}
// display the fraction value
void Fraction::display(std::ostream & stream) const{
  stream << this->numerator.getValue() << "/" << this->denominator.getValue()<<std::endl;
}
//simplify fraction 
const Fraction Fraction::simplify(const Fraction &in){
  int gcd = gcdCalculate(in.numerator.getValue(), in.denominator.getValue());
  Fraction res = Fraction(in.numerator.getValue()/gcd, in.denominator.getValue()/gcd);
  return res;
}
//lcm - least common multiplier
int Fraction::lcmCalculate(const int  lhs, const int  rhs){
  int temp = gcdCalculate(lhs, rhs);
  return temp ? (lhs / temp * rhs) : 0;
}
//gcd - greatest common divisor
int Fraction::gcdCalculate(int a, int  b){
  return b == 0 ? a : gcdCalculate(b, a % b);
}

//frac + frac -- causing problem
boost::tuple<Fraction*, Integer*>/*numRep<Fraction, Integer>*/ Fraction::operator+(const Fraction &rhsIn){
  int numRes, denRes;
  Fraction* resF;
  Integer* resI; //if there is an integer result
  //simplify input 
  Fraction lhs = simplify(*this);
  Fraction rhs = simplify(rhsIn);
  int lcm = lcmCalculate(lhs.denominator.getValue(), rhs.denominator.getValue());
  int gcd = gcdCalculate(lhs.denominator.getValue(), rhs.denominator.getValue());
  //share denominator?
  if(lhs.denominator.getValue() == rhs.denominator.getValue()){
    numRes = lhs.numerator.getValue() + rhs.numerator.getValue();//simply add the numerators
    denRes = lhs.denominator.getValue();//keep denominator
  }
  else{
    //   a1    a2   a1*b2+a2*b1
    //   --  + -- = -----------
    //   b1    b2      b1*b2
    int a1 = lhs.getNumerator();
    int b1 = lhs.getDenominator();
    int a2 = rhs.numerator.getValue();
    int b2 = rhs.denominator.getValue();
    numRes = a1*b2 + a2*b1;
    denRes = b1*b2;
  }
  *resF = Fraction(numRes, denRes);
  //simplify
  *resF = simplify(*resF);
  if(resF->denominator.getValue() == 1){//integer result
    resF->isInteger = true;//flag
    fToI = Integer(resF->numerator.getValue());//make Integer
    resI = &fToI; //return the integer when you can
  }
  else{
    resI = new Integer(0);
  }
  //put the fraction and the (possible) integer representations into a number struct
  resOfAdd = boost::make_tuple(resF, resI);

  std::cout<<" + = ";
  resF->display(std::cout);
  delete resF;
  delete resI;
  return resOfAdd;
}

我必须做错事才能使用struct和tuple来获得相同的段错误。任何人都可以建议我的错误,或建议一个替代/优越的解决方案动态分配返回值?我知道动态灵活的返回类型可能是不可能的。感谢您的时间和帮助。

1 个答案:

答案 0 :(得分:1)

Fraction* resf;
...
*resf =

resf是一个未初始化的指针,您正在尝试复制 - 将内容指定给它所指向的位置。

在这里返回指针是个坏主意,因为它引入了所有权语义。只需按价值返回:

boost::tuple<Fraction, Integer> ...

如果您使用指针以便指示整数是否存在,请考虑使用boost::optional