系统: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 :(得分:0)
你有一个错字。此...
Task LinkedQueue::*dequeue()
应该是
Task* LinkedQueue::dequeue()
这使编译器感到困惑,因为实现与标题中的实现不匹配,并且它不知道它是您的类的一部分。
编辑:此外item
未在任何地方声明,但我认为它应该是Task*