2012-02-19 7 views
1

単純なキュークラスを作成しようとしています。今私は立ち往生している。ほぼ完成しました。私は1つの関数 "pop"だけが問題を引き起こしていることを理解しました。誰でも私に何をすべきか教えてください。ここでキュー作成時のエラークラス

はコードです:
(queue.h)

#ifndef _QUEUE_ 
#define _QUEUE 

#include <iostream> 
#include <string> 
using namespace std; 


struct Stuff 
{ 
    string name; 
    int roll; 
}; 


class Queue 
{ 
private: 

struct Node 
{ 
    Stuff data ; 
    struct Node * next; 
}; 
Node *front; 
Node *back; 
int qsize; 
const int MAX; 
public: 

Queue(int size = 5); 
~Queue(); 

bool isfull() const; 
bool isempty() const; 
int queuesize() const; 

bool push(const Stuff & item); 
bool pop(); 
Stuff first(); 
Stuff last(); 


}; 




#endif 

queue.cpp

#include <iostream> 
using namespace std; 
#include "queue.h" 

Queue::Queue(int size) 
:MAX(size) 
{ 
front = back = 0; 
qsize = 0; 

} 
bool Queue::isempty() const 
{ 
if(qsize == 0) 
    return true; 
else return false; 
} 
bool Queue::isfull() const 
{ 
if(qsize == MAX) 
    return true; 
else 
    return false; 
} 
Queue::~Queue() 
{ 
Node * temp; 
while(front != NULL) 
{ 
    temp = front; 
    front = front->next; 
    delete temp; 
} 

} 
int Queue::queuesize() const 
{ 
return qsize; 
} 

bool Queue::push(const Stuff & swag) 
{ 
if(isfull()) 
    return false; 

Node *add = new Node; 

if(add == NULL) 
    return false; 
add->data = swag; 
add->next = NULL; 

if(front == NULL) 
    front = add; 
else 
    back->next = add->next; 
back = add; 
qsize++; 

return true; 
} 


bool Queue::pop() 
{ 
if(isempty()) 
    return false; 
if(front == NULL) 
    return false; 
Node *temp =front; 


// I think this part is doing something wrong. 
front = front->next; 



delete temp; 
qsize--; 
if(qsize == 0) 
    back = NULL; 
return true; 

} 
Stuff Queue::first() 
{ 
return front->data; 
} 
Stuff Queue::last() 
{ 
return back->data; 
} 

main.cppに

#include <iostream> 
#include "queue.h" 
#include <string> 
using namespace std; 

int main() 
{ 
Queue a(5); 
Stuff data; 
data.name = "icp"; 
data.roll= 755; 
a.push(data); 
Stuff x = a.last(); 

cout << x.name << "\t" << x.roll << endl; 
data.name = "sms"; 
data.roll= 12544; 
a.push(data); 
x = a.last(); 
cout << x.name << "\t" << x.roll << endl; 

data.name = "jmc"; 
data.roll= 98740; 
a.push(data); 
x = a.last(); 
cout << x.name << "\t" << x.roll << endl; 

cout << a.queuesize() << endl; 

///////////// 
x = a.first(); 
cout << x.name << "\t" << x.roll << endl; 
a.pop(); 

x = a.first(); 
cout << x.name << "\t" << x.roll << endl; 
a.pop(); 

x = a.first(); 
cout << x.name << "\t" << x.roll << endl; 
a.pop(); 


//// 
cin.get(); 
cin.get(); 
return 0; 
} 

プログラムがクラッシュ飛び出した後、最初の要素。 私はいくつか問題があると思う部分を指摘しました。事前に感謝:)

+0

デバッガでコードを実行したときに何を学びましたか? –

答えて

3

私は問題がプッシュ機能によって引き起こされると思う。 次のコード

if(front == NULL) 
    front = add; 
else 
    back->next = add->next; 

バック>次にNULLである、次>アドインに設定されています。それはあなたが、フロントがNULLであるため、ポップ機能に問題があるので、front->nextが間違っていると思うback->next = add;

する必要がありますのでadd->next = NULL;

addは、バックアップする次の1です。

これがあなたを助けてくれることを願っています。

+0

ありがとう!出来た。ええ、それは問題を持っていたプッシュ機能でした。小道具! – InspiredCoder

0

動的に割り当てられたオブジェクトへのポインタを持つクラスを使用する場合は、コピーコンストラクタを提供する必要があります。デフォルトの代入演算子は、ビットごとにコピーします。オブジェクトを返すと、コピーされてから削除されます。そのため、有用なデータを含んでいたポインタは破壊されましたが、まだポインタがあります。

デバッガでこのコードを実行しましたが、エラーは文字列(動的に割り当てられた領域へのポインタがあります)にありました。

関連する問題