Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.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++来创建一个简单的队列。 #include "Task5.h" #include <iostream> using namespace std; void push(const long &i, node* &n) { if (n == NULL) { node *ptr = new node; ptr -> item = i; ptr -> next = NULL; n = ptr; cout << "Created New Node." << endl; } else { node *ptr = n; cout << "Created Pointer" << endl; while (ptr -> next != NULL){ cout << "Finding Next..." << endl; ptr = ptr -> next; } cout << "I'm here." << endl; node *temp = new node; temp -> item = i; ptr -> next = temp; cout << "Node Created." << endl; } } long pop(node* &n) { if (n == NULL) cout << "HEY!!! Can't pop an empty queue." << endl; else { long val; node *ptr = n; n = n -> next; val = ptr -> item; delete ptr; return val; } } int main() { node *head = NULL; push(13,head); push(10,head); push(18,head); push(22,head); cout << pop(head) << endl; cout << pop(head) << endl; cout << pop(head) << endl; cout << pop(head) << endl; cout << pop(head) << endl; cout << pop(head) << endl; }_C++_Linux_Output_Cout - Fatal编程技术网

不能给出奇怪的输出 这是我做的一个实验室,它是用C++来创建一个简单的队列。 #include "Task5.h" #include <iostream> using namespace std; void push(const long &i, node* &n) { if (n == NULL) { node *ptr = new node; ptr -> item = i; ptr -> next = NULL; n = ptr; cout << "Created New Node." << endl; } else { node *ptr = n; cout << "Created Pointer" << endl; while (ptr -> next != NULL){ cout << "Finding Next..." << endl; ptr = ptr -> next; } cout << "I'm here." << endl; node *temp = new node; temp -> item = i; ptr -> next = temp; cout << "Node Created." << endl; } } long pop(node* &n) { if (n == NULL) cout << "HEY!!! Can't pop an empty queue." << endl; else { long val; node *ptr = n; n = n -> next; val = ptr -> item; delete ptr; return val; } } int main() { node *head = NULL; push(13,head); push(10,head); push(18,head); push(22,head); cout << pop(head) << endl; cout << pop(head) << endl; cout << pop(head) << endl; cout << pop(head) << endl; cout << pop(head) << endl; cout << pop(head) << endl; }

不能给出奇怪的输出 这是我做的一个实验室,它是用C++来创建一个简单的队列。 #include "Task5.h" #include <iostream> using namespace std; void push(const long &i, node* &n) { if (n == NULL) { node *ptr = new node; ptr -> item = i; ptr -> next = NULL; n = ptr; cout << "Created New Node." << endl; } else { node *ptr = n; cout << "Created Pointer" << endl; while (ptr -> next != NULL){ cout << "Finding Next..." << endl; ptr = ptr -> next; } cout << "I'm here." << endl; node *temp = new node; temp -> item = i; ptr -> next = temp; cout << "Node Created." << endl; } } long pop(node* &n) { if (n == NULL) cout << "HEY!!! Can't pop an empty queue." << endl; else { long val; node *ptr = n; n = n -> next; val = ptr -> item; delete ptr; return val; } } int main() { node *head = NULL; push(13,head); push(10,head); push(18,head); push(22,head); cout << pop(head) << endl; cout << pop(head) << endl; cout << pop(head) << endl; cout << pop(head) << endl; cout << pop(head) << endl; cout << pop(head) << endl; },c++,linux,output,cout,C++,Linux,Output,Cout,因此,最终的结果是代码可以工作,但是它随机输出6296192。我想可能是我拼错了什么,或者是我转换了endl;咒语。我的实验室老师也不知道发生了什么。有人能告诉我发生了什么事吗?如果有帮助,我将通过Linux运行终端运行此代码 提前感谢。我建议在函数中的第一个cout上使用带断点的调试器: long pop(node* &n) { 如果n==NULL为true,则不返回任何内容。这就是UB,也可能导致输出中出现此类随机值。在函数中: long pop(node* &n) {

因此,最终的结果是代码可以工作,但是它随机输出6296192。我想可能是我拼错了什么,或者是我转换了endl;咒语。我的实验室老师也不知道发生了什么。有人能告诉我发生了什么事吗?如果有帮助,我将通过Linux运行终端运行此代码


提前感谢。

我建议在函数中的第一个
cout上使用带断点的调试器:

long pop(node* &n) {
如果
n==NULL
为true,则不返回任何内容。这就是UB,也可能导致输出中出现此类随机值。

在函数中:

long pop(node* &n) {

如果
n==NULL
为true,则不返回任何内容。这就是UB,也可能导致输出中出现这种随机值。

Q:当您尝试弹出一个空队列时,pop()返回什么值?打开编译器警告和/或停止忽略提示您的
pop
函数中“并非所有控制路径都返回值”的警告。这将是第一个线索,那里可能有问题。我希望“实验室讲师也不知道”这句话不是真的,他们确实知道,但希望你自己找到。如果没有,就拿他们给你的几乎所有东西(可能不会太多)。@NathanOliver由于停止问题的不确定性,确实如此。@Quentin我删除了评论,因为我意识到这里只提出了一个警告。“我认为这与停车问题无关,因为每个人都要检查是否所有的出口路径都返回。”NathanOliver说,这是一个过于复杂的问题。只要确定
foo(){bar();}
是否应该触发警告,就需要知道
bar
是否停止。问:当您尝试弹出一个空队列时,pop()返回什么值?打开编译器警告和/或停止忽略提示您“并非所有控制路径都返回值”的警告在您的
pop
功能中。这将是第一个线索,那里可能有问题。我希望“实验室讲师也不知道”这句话不是真的,他们确实知道,但希望你自己找到。如果没有,就拿他们给你的几乎所有东西(可能不会太多)。@NathanOliver由于停止问题的不确定性,确实如此。@Quentin我删除了评论,因为我意识到这里只提出了一个警告。“我认为这与停车问题无关,因为每个人都要检查是否所有的出口路径都返回。”NathanOliver说,这是一个过于复杂的问题。只要确定
foo(){bar();}
是否应该触发警告,就需要知道
bar
是否停止。啊,这很有意义。谢谢我知道这是一件愚蠢的事,XDAh说得通。谢谢我知道这是件愚蠢的事