C++ 重载next;看起来很危险。如果l.head是nullptr呢?你能描述一下你得到了什么吗?崩溃,还是错误的文本?我确实看到了一个可能产生错误文本的bug:out key;并立即取出钥匙。文本将被连接:您是否忘记了逗号?还有,@TEDLYNMO有一个很好的崩溃点。但是当我取消注释键时;我的程序段出现故障和崩溃。这就是你们的问题:电流是nullptr。在取消引用和打印指针指向的内容之前,请检查指针是否为空。如果不想关闭问题,则应使用注释中的说明更新问题。简化此代码,current->key仍然等于-1。当我运行程序时,我调用函数l[2]=LLSortedPosInt2;哪个应该将列表2设置为包含整数2。它会崩溃吗?如果总是将head->key设置为-1,那么第一个节点的键值总是-1就不足为奇了。那个节点有什么用途?那么l[2]是什么?你们有一系列的LLSortedPosInts吗?您还需要显示createNode的代码。编辑您的问题并将该函数放入其中。是的,但我将head->设置在键值旁边。对于给定列表为空的任何情况,我都将head设置为-1,因为head->next==nullptr,如果我编写的代码崩溃,我认为链表不知何故被破坏了。当列表为空时,为什么不让head为null ptr?这就是通常的做法。head->next是nullptr。您的代码不会崩溃,而是像以前一样输出。 struct Node; typedef Node* NodePtr; // The key value HEAD_OF_LIST is used as a "sentinal" value const int HEAD_OF_LIST = -1; class LLSortedPosInt { public: // constructors LLSortedPosInt(); LLSortedPosInt(int key); LLSortedPosInt(int *keys, int n); //int *keys is an array of integers LLSortedPosInt(const LLSortedPosInt &l); // destructor ~LLSortedPosInt(); bool containsElement (int key) const; bool isEmpty ( ) const; LLSortedPosInt& operator= (const LLSortedPosInt &l); bool operator==(const LLSortedPosInt &l) const; bool operator!=(const LLSortedPosInt &l) const; friend LLSortedPosInt operator+ (const LLSortedPosInt &l1, const LLSortedPosInt &l2); friend LLSortedPosInt operator- (const LLSortedPosInt &l1, const LLSortedPosInt &l2); friend ostream& operator<<(ostream &out, const LLSortedPosInt &l); private: void insert (int key); void remove (int key); NodePtr head; }; static NodePtr createNode(int key, NodePtr p) { // allocate a new Node for storing the given key value NodePtr n = new Node; // store the key value and the next pointer n->key = key; n->next = p; // return the new Node to the caller return n; } ostream& operator<< (ostream &out, const LLSortedPosInt &l) { // an empty list will be printed as <> // a singleton list (a list having one key value k) will be // printed as <k> // a list having multiple keys such as 2, 5, 7 will be printed // as <2, 5, 7> // print the left angle bracket out << "<"; NodePtr previous = l.head; NodePtr current = l.head->next; //TEST previous AND current out << previous->key; out << current->key << ">" << endl << "<"; // print the values of l while (current != NULL) { if (current->key >= 0) { if (current->next == NULL) { out << current->key; break; } out << current << ", "; previous = current; current = current->next; } } // print the right angle bracket out << ">"; return out; } LLSortedPosInt::LLSortedPosInt(int key) { // create the sentinal Node at the head of the list head = createNode(HEAD_OF_LIST, nullptr); // add the single element key, as long as it is positive if (key > 0) { head->next = createNode(key, nullptr); } } std::ostream& operator<<(std::ostream& out, const LLSortedPosInt& l) { out << "<"; NodePtr current = l.head; // check that it actually points to a Node before accessing any fields if(current) { out << current->key; // loop through the links and stream the keys while((current = current->next) != nullptr) out << "," << current->key; } out << ">"; return out; } LLSortedPosInt(const int *keys=nullptr, size_t n=0) : head(nullptr) { if(keys && n) { while(n--) { createNode(*keys); ++keys; } } } // single key construction LLSortedPosInt(int key) : LLSortedPosInt(&key, 1) // delegate {} // construction using an initializer list LLSortedPosInt(std::initializer_list<int> il) : LLSortedPosInt(il.begin(), il.size()) // delegate {} NodePtr createNode(int key) { NodePtr current = head; NodePtr previous = nullptr; while(current && (current->key < key)) { previous = current; current = current->next; } if(previous) { // insert the node between previous and previous->next NodePtr rv = new Node(key, previous->next); return previous->next = rv; } else { // insert the node first in the list NodePtr rv = new Node(key, head); return head = rv; } } struct Node { Node* next; int key; Node(int k, Node* n) : next(n), key(k) {} Node(const Node&) = delete; Node(Node&&) = delete; Node& operator=(const Node&) = delete; Node& operator=(Node&&) = delete; ~Node() = default; }; using NodePtr = Node*;
我试图输出我的链表的值,但我似乎无法获得运算符的定义。运算符在哪里声明?到底是什么不起作用?第一个电流=l.head->next;看起来很危险。如果l.head是nullptr呢?你能描述一下你得到了什么吗?崩溃,还是错误的文本?我确实看到了一个可能产生错误文本的bug:out key;并立即取出钥匙。文本将被连接:您是否忘记了逗号?还有,@TEDLYNMO有一个很好的崩溃点。但是当我取消注释键时;我的程序段出现故障和崩溃。这就是你们的问题:电流是nullptr。在取消引用和打印指针指向的内容之前,请检查指针是否为空。如果不想关闭问题,则应使用注释中的说明更新问题。简化此代码,current->key仍然等于-1。当我运行程序时,我调用函数l[2]=LLSortedPosInt2;哪个应该将列表2设置为包含整数2。它会崩溃吗?如果总是将head->key设置为-1,那么第一个节点的键值总是-1就不足为奇了。那个节点有什么用途?那么l[2]是什么?你们有一系列的LLSortedPosInts吗?您还需要显示createNode的代码。编辑您的问题并将该函数放入其中。是的,但我将head->设置在键值旁边。对于给定列表为空的任何情况,我都将head设置为-1,因为head->next==nullptr,如果我编写的代码崩溃,我认为链表不知何故被破坏了。当列表为空时,为什么不让head为null ptr?这就是通常的做法。head->next是nullptr。您的代码不会崩溃,而是像以前一样输出。C++ 重载next;看起来很危险。如果l.head是nullptr呢?你能描述一下你得到了什么吗?崩溃,还是错误的文本?我确实看到了一个可能产生错误文本的bug:out key;并立即取出钥匙。文本将被连接:您是否忘记了逗号?还有,@TEDLYNMO有一个很好的崩溃点。但是当我取消注释键时;我的程序段出现故障和崩溃。这就是你们的问题:电流是nullptr。在取消引用和打印指针指向的内容之前,请检查指针是否为空。如果不想关闭问题,则应使用注释中的说明更新问题。简化此代码,current->key仍然等于-1。当我运行程序时,我调用函数l[2]=LLSortedPosInt2;哪个应该将列表2设置为包含整数2。它会崩溃吗?如果总是将head->key设置为-1,那么第一个节点的键值总是-1就不足为奇了。那个节点有什么用途?那么l[2]是什么?你们有一系列的LLSortedPosInts吗?您还需要显示createNode的代码。编辑您的问题并将该函数放入其中。是的,但我将head->设置在键值旁边。对于给定列表为空的任何情况,我都将head设置为-1,因为head->next==nullptr,如果我编写的代码崩溃,我认为链表不知何故被破坏了。当列表为空时,为什么不让head为null ptr?这就是通常的做法。head->next是nullptr。您的代码不会崩溃,而是像以前一样输出。 struct Node; typedef Node* NodePtr; // The key value HEAD_OF_LIST is used as a "sentinal" value const int HEAD_OF_LIST = -1; class LLSortedPosInt { public: // constructors LLSortedPosInt(); LLSortedPosInt(int key); LLSortedPosInt(int *keys, int n); //int *keys is an array of integers LLSortedPosInt(const LLSortedPosInt &l); // destructor ~LLSortedPosInt(); bool containsElement (int key) const; bool isEmpty ( ) const; LLSortedPosInt& operator= (const LLSortedPosInt &l); bool operator==(const LLSortedPosInt &l) const; bool operator!=(const LLSortedPosInt &l) const; friend LLSortedPosInt operator+ (const LLSortedPosInt &l1, const LLSortedPosInt &l2); friend LLSortedPosInt operator- (const LLSortedPosInt &l1, const LLSortedPosInt &l2); friend ostream& operator<<(ostream &out, const LLSortedPosInt &l); private: void insert (int key); void remove (int key); NodePtr head; }; static NodePtr createNode(int key, NodePtr p) { // allocate a new Node for storing the given key value NodePtr n = new Node; // store the key value and the next pointer n->key = key; n->next = p; // return the new Node to the caller return n; } ostream& operator<< (ostream &out, const LLSortedPosInt &l) { // an empty list will be printed as <> // a singleton list (a list having one key value k) will be // printed as <k> // a list having multiple keys such as 2, 5, 7 will be printed // as <2, 5, 7> // print the left angle bracket out << "<"; NodePtr previous = l.head; NodePtr current = l.head->next; //TEST previous AND current out << previous->key; out << current->key << ">" << endl << "<"; // print the values of l while (current != NULL) { if (current->key >= 0) { if (current->next == NULL) { out << current->key; break; } out << current << ", "; previous = current; current = current->next; } } // print the right angle bracket out << ">"; return out; } LLSortedPosInt::LLSortedPosInt(int key) { // create the sentinal Node at the head of the list head = createNode(HEAD_OF_LIST, nullptr); // add the single element key, as long as it is positive if (key > 0) { head->next = createNode(key, nullptr); } } std::ostream& operator<<(std::ostream& out, const LLSortedPosInt& l) { out << "<"; NodePtr current = l.head; // check that it actually points to a Node before accessing any fields if(current) { out << current->key; // loop through the links and stream the keys while((current = current->next) != nullptr) out << "," << current->key; } out << ">"; return out; } LLSortedPosInt(const int *keys=nullptr, size_t n=0) : head(nullptr) { if(keys && n) { while(n--) { createNode(*keys); ++keys; } } } // single key construction LLSortedPosInt(int key) : LLSortedPosInt(&key, 1) // delegate {} // construction using an initializer list LLSortedPosInt(std::initializer_list<int> il) : LLSortedPosInt(il.begin(), il.size()) // delegate {} NodePtr createNode(int key) { NodePtr current = head; NodePtr previous = nullptr; while(current && (current->key < key)) { previous = current; current = current->next; } if(previous) { // insert the node between previous and previous->next NodePtr rv = new Node(key, previous->next); return previous->next = rv; } else { // insert the node first in the list NodePtr rv = new Node(key, head); return head = rv; } } struct Node { Node* next; int key; Node(int k, Node* n) : next(n), key(k) {} Node(const Node&) = delete; Node(Node&&) = delete; Node& operator=(const Node&) = delete; Node& operator=(Node&&) = delete; ~Node() = default; }; using NodePtr = Node*;,c++,operator-overloading,overloading,nodes,operator-keyword,C++,Operator Overloading,Overloading,Nodes,Operator Keyword,我试图输出我的链表的值,但我似乎无法获得运算符的定义。运算符在哪里声明?到底是什么不起作用?第一个电流=l.head->next;看起来很危险。如果l.head是nullptr呢?你能描述一下你得到了什么吗?崩溃,还是错误的文本?我确实看到了一个可能产生错误文本的bug:out key;并立即取出钥匙。文本将被连接:您是否忘记了逗号?还有,@TEDLYNMO有一个很好的崩溃点。但是当我取消注释键时;我的程序段出现故障和崩溃。这就是你们的问题:电流是nullptr。在取消引用和打印指针指向的内容之
struct Node;
typedef Node* NodePtr;
// The key value HEAD_OF_LIST is used as a "sentinal" value
const int HEAD_OF_LIST = -1;
class LLSortedPosInt {
public:
// constructors
LLSortedPosInt();
LLSortedPosInt(int key);
LLSortedPosInt(int *keys, int n); //int *keys is an array of integers
LLSortedPosInt(const LLSortedPosInt &l);
// destructor
~LLSortedPosInt();
bool containsElement (int key) const;
bool isEmpty ( ) const;
LLSortedPosInt& operator= (const LLSortedPosInt &l);
bool operator==(const LLSortedPosInt &l) const;
bool operator!=(const LLSortedPosInt &l) const;
friend LLSortedPosInt operator+ (const LLSortedPosInt &l1,
const LLSortedPosInt &l2);
friend LLSortedPosInt operator- (const LLSortedPosInt &l1,
const LLSortedPosInt &l2);
friend ostream& operator<<(ostream &out,
const LLSortedPosInt &l);
private:
void insert (int key);
void remove (int key);
NodePtr head;
};
static NodePtr createNode(int key, NodePtr p) {
// allocate a new Node for storing the given key value
NodePtr n = new Node;
// store the key value and the next pointer
n->key = key;
n->next = p;
// return the new Node to the caller
return n;
}
ostream& operator<< (ostream &out, const LLSortedPosInt &l) {
// an empty list will be printed as <>
// a singleton list (a list having one key value k) will be
// printed as <k>
// a list having multiple keys such as 2, 5, 7 will be printed
// as <2, 5, 7>
// print the left angle bracket
out << "<";
NodePtr previous = l.head;
NodePtr current = l.head->next;
//TEST previous AND current
out << previous->key;
out << current->key << ">" << endl << "<";
// print the values of l
while (current != NULL) {
if (current->key >= 0) {
if (current->next == NULL) {
out << current->key;
break;
}
out << current << ", ";
previous = current;
current = current->next;
}
}
// print the right angle bracket
out << ">";
return out;
}
LLSortedPosInt::LLSortedPosInt(int key) {
// create the sentinal Node at the head of the list
head = createNode(HEAD_OF_LIST, nullptr);
// add the single element key, as long as it is positive
if (key > 0) {
head->next = createNode(key, nullptr);
}
}
std::ostream& operator<<(std::ostream& out, const LLSortedPosInt& l) {
out << "<";
NodePtr current = l.head;
// check that it actually points to a Node before accessing any fields
if(current) {
out << current->key;
// loop through the links and stream the keys
while((current = current->next) != nullptr)
out << "," << current->key;
}
out << ">";
return out;
}
LLSortedPosInt(const int *keys=nullptr, size_t n=0) :
head(nullptr)
{
if(keys && n) {
while(n--) {
createNode(*keys);
++keys;
}
}
}
// single key construction
LLSortedPosInt(int key) :
LLSortedPosInt(&key, 1) // delegate
{}
// construction using an initializer list
LLSortedPosInt(std::initializer_list<int> il) :
LLSortedPosInt(il.begin(), il.size()) // delegate
{}
NodePtr createNode(int key) {
NodePtr current = head;
NodePtr previous = nullptr;
while(current && (current->key < key)) {
previous = current;
current = current->next;
}
if(previous) {
// insert the node between previous and previous->next
NodePtr rv = new Node(key, previous->next);
return previous->next = rv;
} else {
// insert the node first in the list
NodePtr rv = new Node(key, head);
return head = rv;
}
}
struct Node {
Node* next;
int key;
Node(int k, Node* n) : next(n), key(k) {}
Node(const Node&) = delete;
Node(Node&&) = delete;
Node& operator=(const Node&) = delete;
Node& operator=(Node&&) = delete;
~Node() = default;
};
using NodePtr = Node*;