2013-02-27 64 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