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
4

1 回答 1

0

你有一个错字。这个...

Task LinkedQueue::*dequeue()

应该

Task* LinkedQueue::dequeue()

这使编译器感到困惑,因为实现与您在标头中的内容不匹配,并且它不知道它是您类的一部分。

编辑:此外item没有在任何地方声明,但我认为它应该是类型的局部变量Task*

于 2013-02-27T06:07:11.437 回答