C++ C++;错误消息
我得到以下函数的错误消息分段错误。我想知道是否有人能帮我解决这个问题C++ C++;错误消息,c++,linked-list,polynomials,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
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;
}
我敢打赌,您遇到了分段错误,因为在
while
语句的条件中没有正确的测试
您正在使用temporary->m_next!=空
。当临时
为空
时会发生什么
而不是
while(temporary -> m_next != NULL){
copy = p.m_head;
while(copy -> m_next != NULL){
你应该使用
while(temporary != NULL){
copy = p.m_head;
while(copy != NULL){
分段错误具体发生在哪里?您确定
m_head
在输入函数时不为空吗?p.m_head怎么样?我只想用一个std::vector而不是链表。如果你觉得你必须使用一个链表,那么有一个std::list
适合你。问题解决了,谢谢!我确实在开始时将m_head初始化为null。