Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
超载+;和-操作员,很难弄清楚如何检查;“金额”;被传进来是负面的 我已经重载了这些操作符来帮助我遍历一个双链表,但是已经遇到了一个小错误,而且C++仍然是新的。我从未考虑过,输入的“金额”将是一个负数。所以我想我需要在每个操作符中检查一个负数,因为它将极大地改变我遍历列表的方式,例如,如果我指向节点5和I+(-3),我希望它向后移动三个节点,与-,5-(-3)相同的是向前移动三个节点。逻辑似乎很简单,但语法很混乱。以下是重载的运算符: template <typename T> typename doublyLinkedList<T>::iterator doublyLinkedList<T>::iterator::operator+(const int amount) const { doublyLinkedList<T>::iterator tempClone(*this); tempClone.pastBoundary=false; T i; for(i=0; i < amount; i++) { if(tempClone.current->forward == NULL) { tempClone.pastBoundary =true; }else { ++tempClone; } } if(tempClone.pastBoundary == true) { return *this; }else { return tempClone; } } template <typename T> typename doublyLinkedList<T>::iterator doublyLinkedList<T>::iterator::operator-(const int amount) const { doublyLinkedList<T>::iterator tempClone(*this); tempClone.pastBoundary=false; T i; for(i=0; i < amount; i++) { if(tempClone.current->backward == NULL) { tempClone.pastBoundary =true; }else { --tempClone; } } if(tempClone.pastBoundary == true) { return *this; }else { return tempClone; } } 模板 typename doublyLinkedList::iterator doublyLinkedList::iterator::operator+(常量整数金额)常量{ 双链接列表::迭代器tempClone(*this); tempClone.pastBoundary=false; TⅠ; 对于(i=0;iforward==NULL) { tempClone.passboundary=true; }否则 { ++tempClone; } } if(tempClone.passboundary==true) { 归还*这个; }否则 { 返回tempClone; } } 模板 typename DoublyLink列表::迭代器DoublyLink列表::迭代器::运算符-(常量int数量)常量{ 双链接列表::迭代器tempClone(*this); tempClone.pastBoundary=false; TⅠ; 对于(i=0;ibackward==NULL) { tempClone.passboundary=true; }否则 { --tempClone; } } if(tempClone.passboundary==true) { 归还*这个; }否则 { 返回tempClone; } }_C++_Linked List_Singly Linked List - Fatal编程技术网

超载+;和-操作员,很难弄清楚如何检查;“金额”;被传进来是负面的 我已经重载了这些操作符来帮助我遍历一个双链表,但是已经遇到了一个小错误,而且C++仍然是新的。我从未考虑过,输入的“金额”将是一个负数。所以我想我需要在每个操作符中检查一个负数,因为它将极大地改变我遍历列表的方式,例如,如果我指向节点5和I+(-3),我希望它向后移动三个节点,与-,5-(-3)相同的是向前移动三个节点。逻辑似乎很简单,但语法很混乱。以下是重载的运算符: template <typename T> typename doublyLinkedList<T>::iterator doublyLinkedList<T>::iterator::operator+(const int amount) const { doublyLinkedList<T>::iterator tempClone(*this); tempClone.pastBoundary=false; T i; for(i=0; i < amount; i++) { if(tempClone.current->forward == NULL) { tempClone.pastBoundary =true; }else { ++tempClone; } } if(tempClone.pastBoundary == true) { return *this; }else { return tempClone; } } template <typename T> typename doublyLinkedList<T>::iterator doublyLinkedList<T>::iterator::operator-(const int amount) const { doublyLinkedList<T>::iterator tempClone(*this); tempClone.pastBoundary=false; T i; for(i=0; i < amount; i++) { if(tempClone.current->backward == NULL) { tempClone.pastBoundary =true; }else { --tempClone; } } if(tempClone.pastBoundary == true) { return *this; }else { return tempClone; } } 模板 typename doublyLinkedList::iterator doublyLinkedList::iterator::operator+(常量整数金额)常量{ 双链接列表::迭代器tempClone(*this); tempClone.pastBoundary=false; TⅠ; 对于(i=0;iforward==NULL) { tempClone.passboundary=true; }否则 { ++tempClone; } } if(tempClone.passboundary==true) { 归还*这个; }否则 { 返回tempClone; } } 模板 typename DoublyLink列表::迭代器DoublyLink列表::迭代器::运算符-(常量int数量)常量{ 双链接列表::迭代器tempClone(*this); tempClone.pastBoundary=false; TⅠ; 对于(i=0;ibackward==NULL) { tempClone.passboundary=true; }否则 { --tempClone; } } if(tempClone.passboundary==true) { 归还*这个; }否则 { 返回tempClone; } }

超载+;和-操作员,很难弄清楚如何检查;“金额”;被传进来是负面的 我已经重载了这些操作符来帮助我遍历一个双链表,但是已经遇到了一个小错误,而且C++仍然是新的。我从未考虑过,输入的“金额”将是一个负数。所以我想我需要在每个操作符中检查一个负数,因为它将极大地改变我遍历列表的方式,例如,如果我指向节点5和I+(-3),我希望它向后移动三个节点,与-,5-(-3)相同的是向前移动三个节点。逻辑似乎很简单,但语法很混乱。以下是重载的运算符: template <typename T> typename doublyLinkedList<T>::iterator doublyLinkedList<T>::iterator::operator+(const int amount) const { doublyLinkedList<T>::iterator tempClone(*this); tempClone.pastBoundary=false; T i; for(i=0; i < amount; i++) { if(tempClone.current->forward == NULL) { tempClone.pastBoundary =true; }else { ++tempClone; } } if(tempClone.pastBoundary == true) { return *this; }else { return tempClone; } } template <typename T> typename doublyLinkedList<T>::iterator doublyLinkedList<T>::iterator::operator-(const int amount) const { doublyLinkedList<T>::iterator tempClone(*this); tempClone.pastBoundary=false; T i; for(i=0; i < amount; i++) { if(tempClone.current->backward == NULL) { tempClone.pastBoundary =true; }else { --tempClone; } } if(tempClone.pastBoundary == true) { return *this; }else { return tempClone; } } 模板 typename doublyLinkedList::iterator doublyLinkedList::iterator::operator+(常量整数金额)常量{ 双链接列表::迭代器tempClone(*this); tempClone.pastBoundary=false; TⅠ; 对于(i=0;iforward==NULL) { tempClone.passboundary=true; }否则 { ++tempClone; } } if(tempClone.passboundary==true) { 归还*这个; }否则 { 返回tempClone; } } 模板 typename DoublyLink列表::迭代器DoublyLink列表::迭代器::运算符-(常量int数量)常量{ 双链接列表::迭代器tempClone(*this); tempClone.pastBoundary=false; TⅠ; 对于(i=0;ibackward==NULL) { tempClone.passboundary=true; }否则 { --tempClone; } } if(tempClone.passboundary==true) { 归还*这个; }否则 { 返回tempClone; } },c++,linked-list,singly-linked-list,C++,Linked List,Singly Linked List,在operator+开头添加: if (amount <0) { operator-(-amount); return; } 将-amount赋值给amount,然后测试amount是否等于零 在运算符+的开头,添加: if (amount <0) { operator-(-amount); return; } 将-amount赋值给amount,然后测试amount是否等于零 如果(amount=(-amount))-除非amount为0,否则始终为真 它需要在

在operator+开头添加:

if (amount <0) {
  operator-(-amount);
  return;
}

将-amount赋值给amount,然后测试amount是否等于零

在运算符+的开头,添加:

if (amount <0) {
  operator-(-amount);
  return;
}
将-amount赋值给amount,然后测试amount是否等于零

如果(amount=(-amount))
-除非amount为0,否则始终为真

它需要在for循环之前运行。事实上,我可能会:

if (amount < 0) return this->operator-(-amount); 
如果(金额<0)返回此->运算符-(-amount);
对于另一个操作员,反之亦然

如果(amount=(-amount))
-除非amount为0,否则始终为真

它需要在for循环之前运行。事实上,我可能会:

if (amount < 0) return this->operator-(-amount); 
如果(金额<0)返回此->运算符-(-amount);

对于另一个操作员,反之亦然

哦,是的,我忘了我把它忘在那里了。那只是一张我盯着的便条,提醒我想让if做什么。谢谢。哦,是的,我忘了我把它忘在那里了。那只是我盯着的一张便条,提醒我要if做什么。非常感谢。