我正在检查构造函数中头节点的数据及其确定,但是当我在函数中检查它时它就消失了。为什么这个幽灵这个指针?
队列标题
E/Web Console﹕ Uncaught TypeError: Object #<Screen> has no method 'lockOrientation'
队列类
#ifndef QUEUE_H
#define QUEUE_H
#include "Node.h"
#include "LinkedList.h"
class Queue
{
public:
Queue();
~Queue();
void addTail(Node* newNode);
Node* deleteHead();
int isEmpty();
void PrintQueue(char* outName);
protected:
private:
Node* head;
Node* tail;
LinkedList* myList;
};
#endif // QUEUE_H
主要功能
#include "Queue.h"
Queue::Queue()
{
//constructor
myList = new LinkedList();
head = myList->getHead();
std::cout<<" head here is " << head->getData()<<std::endl; //
std::cout<<" next after head is " << head->getNext()->getData()<<std::endl; //
tail = myList->getTail();
}
void Queue::addTail(Node* newNode)
{
std::cout<<"inserting before tail (queue)"<<std::endl;
std::cout<<"new node is " << newNode->getData() <<std::endl;
std::cout<<" head here is " << myList->getHead()->getData() <<std::endl;
myList->insertLast(newNode);
}
Node* Queue::deleteHead()
{
if(isEmpty()){
std::cout<<"Queue is empty. Cannot remove anything anymore. Add more data to the queue!"<<std::endl;
return head;
}
return myList->removeFirst();
}
int Queue::isEmpty()
{
return myList->isEmpty();
}
void Queue::PrintQueue(char* outName)
{
Node* cur = head->getNext(); //store current node to keep track. Set it to one after the head
std::ofstream outfile;
outfile.open(outName, std::ios::app);
outfile << "Printing the values of the queue: ";
std::cout<<"Printing the values of the queue: "<<std::endl;
while(cur != tail)
{
std::cout<< cur->getData()<<", "<<std::endl; //print to the console
outfile << cur->getData() <<", "; //print to file
}
outfile.close();
}
Queue::~Queue()
{
//destructor
delete myList;
}
链接列表
#include <iostream>
#include <fstream>
#include <string>
#include "Stack.h"
//#include "Stack.cpp"
#include "LinkedList.h"
//#include "LinkedList.cpp"
#include "Node.h"
//#include "Node.cpp"
#include "HashTable.h"
//#include "HashTable.cpp"
using namespace std;
int main( int argc, char* argv[] )
{
//specifying the in and out files
char* inFileName = argv[1];
char* outFileName = argv[2];
std::fstream infile (inFileName) ; // input file
//open the input file and find largest integer
int num;
int largest = 0;
Stack * myStack = new Stack();
char buffer[33]; //create a buffer for using with itoa
std::ofstream outfile;
outfile.open(outFileName);
if ( !infile.is_open() ) //check if input file is open
cout<<"Could not open file\n";
else {
while (infile >> num) { // read file int by int and check if current is not the largest
if(num > largest) largest = num;
Node *newNode= new Node(itoa(num,buffer,10));
myStack->push(newNode);
}
}
std::cout<< std::endl;
infile.close(); //close files that you read to avoid memory leaks
myStack->PrintStack(outFileName);
HashTable* hashBrown = new HashTable();
int currentDigit = 0;
int currentTable = 0;
int numOfDig =0; //stores number of digits of the largest number
string maxNum = itoa(largest,buffer,10);
numOfDig = maxNum.length();
std::cout<< "Num of digits " << numOfDig << std::endl;
Node* current;
while(!myStack->isEmpty())
{
current = myStack->pop();
std::cout<< "pop from stack element " << current->getData() << std::endl;
string str = current->getData();
int index = atoi(&str.back());
std::cout<< "insert at index " << index << std::endl;
std::cout<< "inserting data: "<< current->getData()<< " at index:" << index << std::endl;
hashBrown->myQueues[index].addTail(current);
}
hashBrown->printHashTable(outFileName);
delete myStack;
delete hashBrown;
outfile.close();
std::cout<< "finishing program " << std::endl;
return 0;
}
所以一切正常,直到它尝试访问Queue的addTail()函数中的头部或其数据。
答案 0 :(得分:2)
您可以使用gdb
调试程序,查找错误并修复它们。如果程序有核心,您可以使用backtrace
来查看导致核心的原因。
简短的gdb tutorial