C ++错误消息

时间:2015-10-29 02:07:26

标签: c++ linked-list polynomials

我收到以下函数的错误消息分段错误。我想知道是否有人可以帮我解决这个问题

Polynomial Polynomial :: multiply(const Polynomial& p) const{
  //The functions returns the mathematical product of the host polynomial and p

  // new object created
  Polynomial multiply;
  // node that points to the beginning of the list
  Node *temporary = m_head;

  Node *copy = p.m_head;

  long num_coeff;

  unsigned int num_deg;

  while(temporary -> m_next != NULL){
    copy = p.m_head;
    while(copy -> m_next != NULL){
      Polynomial variable;
      num_coeff = temporary -> m_coefficient * copy -> m_coefficient;
      num_deg = temporary -> m_degree + copy -> m_degree;
      variable.insertMonomial(num_coeff, num_deg);
      multiply.add(variable);
      copy = copy -> m_next;
    }
    temporary = temporary -> m_next;
  }
  return multiply; 
}

1 个答案:

答案 0 :(得分:0)

我愿意打赌您遇到了分段错误,因为您在while语句的条件中没有正确的测试。

您正在使用temporary -> m_next != NULLtemporaryNULL时会发生什么?

而不是

while(temporary -> m_next != NULL){
   copy = p.m_head;
   while(copy -> m_next != NULL){

你应该使用

while(temporary != NULL){
   copy = p.m_head;
   while(copy != NULL){