2013-02-27 67 views
0

系統:Linux 編譯器:GCC 4.4.6版問題與函數指針在出隊函數用於鏈表隊列

計劃是在教師指定的要使用的功能的大專水平班提供課程。我只能對這兩個文件進行更改,而不能對教師提供的任何文件進行更改。該程序似乎運行正常,除了我的實現出列函數。我需要訪問前面,後面,項目和計數。編譯器說他們超出了範圍。我能夠從cpp文件中的其他功能訪問它們,但由於不同的方法,這個使用我很難過。從我發現這是一個函數指針。我從來沒有與這些工作過,唯一的文檔,我可以找到他們是如何使用它們,但沒有關於如何從函數外部訪問成員沒有發送英寸任何幫助,將不勝感激。我一直在爲此工作數日,無法決定如何解決問題。

//////////////////////////////////////////////////////////////////////////////////////////// 

//My.h 
#ifndef __LINKEDQUEUE_H__ 
#define __LINKEDQUEUE_H__ 

#include <ostream> 
#include <stdint.h> 
#include "task.h" 
#include "queueExceptions.h" 

#include <string> 
#include <new> 
#include "queue.h" 

/*class QueueEmpty 
{}; 

class QueueFull 
{}; 
*/ 

typedef Task* ItemType; 


struct NodeType { 
    ItemType info; 
    NodeType* next; 
}; 

class LinkedQueue: public Queue 
{ 
public: 
    int count; 
    NodeType* front; 
    NodeType* rear; 


    LinkedQueue(); 

    ~LinkedQueue(); 

    /** 
    * Enqueue a task onto the queue 
    * @param tsk The task to enqueue 
    * @throws QueueFull if there is not room on the queue to place the item. 
    */ 
    void enqueue(Task *tsk) throw (QueueFull); 

    /** 
    * Dequeue an element from the queue. 
    * @return the front of the queue. 
    * @throws QueueEmpty if there are no elements in the queue. 
    */ 
    Task *dequeue() throw (QueueEmpty); 

    /** 
    * Retrieve the current number of items on the queue. 
    * @return the current number of items on the queue. 
    */ 
    size_t depth() const; 


}; 
#endif // __LINKEDQUEUE_H__ 

/////////////////////// ////////////////////////////

//my.cpp 

#include <iostream> 
#include <string> 
#include "linkedQueue.h" 

using namespace std; 

LinkedQueue::LinkedQueue() 
{ 
    front = NULL; 
    rear = NULL; 
    count = 0; 
} 

    /** 
    * Enqueue a task onto the queue 
    * @param tsk The task to enqueue 
    * @throws QueueFull if there is not room on the queue to place the item. 
    */ 
    void LinkedQueue::enqueue(Task *tsk) throw (QueueFull) 
{ 
    NodeType* newNode; 
    newNode = new NodeType; 
    if (newNode == NULL) 
    { 
    throw QueueFull(); 
    } 
    else 
    { 
    newNode->info = tsk; 
    newNode->next = NULL; 
    if (rear == NULL) 
    front = newNode; 
    else 
    rear->next = newNode; 
    rear = newNode; 
    count++; 
    } 
} 
    /** 
    * Dequeue an element from the queue. 
    * @return the front of the queue. 
    * @throws QueueEmpty if there are no elements in the queue. 
    */ 
    Task LinkedQueue::*dequeue() throw (QueueEmpty) 
{ 
    if (front == NULL) {throw QueueEmpty();} 
    else 
    { 
    NodeType* tempPtr; 
    tempPtr = front; 
    item = front->info; 
    front = front->next; 
    if (front == NULL) 
     rear = NULL; 
    delete tempPtr; 
    LinkedQueue::count--; 
    return item; 

    } 
} 

    /** 
    * Retrieve the current number of items on the queue. 
    * @return the current number of items on the queue. 
    */ 
    size_t LinkedQueue::depth() const 
{ 
    return count; 
} 

    LinkedQueue::~LinkedQueue() 
{ 
NodeType* tempPtr; 
while (front != NULL) 
    { 
    tempPtr = front; 
    front = front->next; 
    delete tempPtr; 
    } 
rear = NULL; 
} 

//////////////////////////////////////////////////////////////////////////////////////////// 

compile errors: 
g++ -g -o linkedQueue.o -Wall -Werror -c linkedQueue.cpp 
linkedQueue.cpp: In function ‘Task LinkedQueue::* dequeue()’: 
linkedQueue.cpp:46: error: ‘front’ was not declared in this scope 
linkedQueue.cpp:51: error: ‘item’ was not declared in this scope 
linkedQueue.cpp:54: error: ‘rear’ was not declared in this scope 
linkedQueue.h:31: error: invalid use of non-static data member ‘LinkedQueue::count’ 
linkedQueue.cpp:56: error: from this location 
make: *** [linkedQueue.o] Error 1 
+0

歡迎堆棧溢出。如果你打算髮布這樣一個長長的問題和完全賦予你的任務的代碼,沒有人會有興趣去看看它。相反,要確切地說明你到目前爲止發現的問題以及你遇到的問題/問題。同時編輯帖子中的代碼,只顯示與您想要回答的特定問題相關的部分。 – Tuxdude 2013-02-27 06:22:20

回答

0

你有一個錯字。這...

Task LinkedQueue::*dequeue() 

應該

Task* LinkedQueue::dequeue() 

這混淆了編譯器實施以來,不符合你有你的頭,它不知道這是你的類的一部分。

編輯:另外item不被任何聲明,但我認爲它的意思是類型的局部變量Task*

+0

我不敢相信我錯過了那個。非常感謝wilsonmichaelpatrick的幫助。我會繼續爲那一個踢自己。 – 2013-02-27 06:21:22