我遇到一个问题,无法弄清楚如何诊断。自从我从事C ++工作以来已经有一段时间了,我决定编写一个基于类的LLL实现,出于实践的考虑,其中一个类用于节点,一个类用于列表。一旦在main中初始化了列表,它就会提示用户从构造函数中输入列表的长度。然后,由于某种原因,它挂断而不是生成列表。我还没有得到任何错误代码,而且据我所知,它似乎并没有被卡在循环中。我很困惑
主要功能:
int main() {
LLL * myList = new LLL();
int displayCount = 0;
displayCount = myList->display();
cout << "\n\n" << displayCount << " nodes were displayed\n\n";
delete myList;
return 0;
}
LLL构造函数:
LLL::LLL() {
head = new node(rand() % 20);
node * current = head;
cout << "\n\nHow many nodes would you like this list to be? ";
int length = 0;
cin >> length;
cin.ignore(1000);
for (int i = 1; i <= length; ++i) {
node * temp = new node(rand() % 20);
current->attachNext(temp);
current = temp;
delete temp;
}
节点构造函数:
node::node(int data) {
this->next = NULL;
this->data = data;
}
attachNext功能:
bool node::attachNext(node *& toAttach) {
this->next = toAttach;
return true;
}
头文件:
#include <iostream>
#include <math.h>
using namespace std;
class node {
public:
node();
node(int data);
~node();
node * traverse();//returns obj->next node
bool checkNext();//returns true if obj->next exists
bool attachNext(node *& toAttach);
int display();
int deleteAll(int & count);
private:
node * next;
int data;
};
class LLL {
public:
LLL();
LLL(int length);
~LLL();
int display();
private:
node * head;
};
答案 0 :(得分:0)
好,我知道了。我在LLL中调用了cin.ignore函数,却忘记了指定分隔符。
此:
cin.ignore(1000);
应该是:
cin.ignore(1000, '\n');