【发布时间】:2015-10-16 06:53:43
【问题描述】:
这是 C++ 中的链接结构。这里的所有变量和对象都应该有定义的地址。但是,执行中却另有说明。
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
struct LineData {
int ID;
char name[33];
LineData *next;
};
class Line {
private:
LineData *pointerHead;
LineData *pointerToTail;
int numObjects;
public:
Line();
~Line();
void enterLine(int ID, char *data);
void enterLine(LineData *lindat);
LineData *exitLine();
int count();
};
Line::Line() {
pointerHead = NULL;
pointerToTail = NULL;
numObjects = 0;
/*pointerHead = NULL;
pointerToTail = NULL;*/
}
/** Puts an item into the line. **/
void Line::enterLine(int ID, char *data) {
LineData *temp;
cout << "Inner sanctums called.\n";
temp = new LineData;
temp->ID = ID;
strcpy(temp->name, data);
temp->next = NULL;
cout << "Temp created.\n\n";
//cout << "pointerHead is... ... " << pointerHead << "!\n"; Not going to work!
/* Insert into the Line */
if(pointerHead != 0) { /* Insert as first in this Line/You shall not pass! Program will not let you pass! BOOM! Crash! */
cout << "If tried.\n";
pointerToTail->next = temp;
pointerToTail = temp;
}
else /* Insert at Tail of the Line */
{
cout << "pointerHead == NULL. What are you going to do about it?\n";
cout << temp->ID << "\n";
pointerHead = temp;
pointerToTail = temp;
cout << "Inserted into Line!\n";
}
}
/** ......
......
..........
..........
...... **/
正确编译。
int main() {
Line *GeorgiaCyclone;
Line *GiletteIce; // Shaved Ice.
Line *WoodenMagic;
Line *SteelCityNinja;
Line *Egbe; // Hawk.
GeorgiaCyclone->enterLine(4, "Preston");
Egbe->enterLine(2, "Felix");
return 181;
}
每行的地址在创建时设置为 NULL。使用this 并没有什么不同。这怎么可能发生?如何缓解这种崩溃?
【问题讨论】:
-
“链接结构”?
-
“这里所有的变量和对象都应该有定义的地址。” 什么对象?从字面上看,您的顶级指针都没有指向任何东西。
标签: c++ pointers linked-list null queue