- 论坛徽章:
- 0
|
此程序在Linux编译则报如下错误:
In file included from fig15_12.cpp:4:
queue.h: In member function `bool Queue<QUEUETYPE>::isQueueEmpty() const':
queue.h:15: error: there are no arguments to `isEmpty' that depend on a template parameter, so a declaration of `isEmpty' must be available
queue.h:15: error: (if you use `-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
queue.h: In member function `void Queue<QUEUETYPE>::printQueue() const':
queue.h:16: error: there are no arguments to `print' that depend on a template parameter, so a declaration of `print' must be available
而在visual C++6则顺利编译。问题好像是处在模板上,究竟是什么原因,以及该如何修改。求教。
fig15_12.cpp
// Fig. 15.12: fig15_12.cpp
// Driver to test the template Queue class
#include <iostream>
#include "queue_c.h"
using std::endl;
int main()
{
Queue< int > intQueue;
int dequeueInteger, i;
cout << "processing an integer Queue" << endl;
for ( i = 0; i < 4; i++ ) {
intQueue.enqueue( i );
intQueue.printQueue();
}
while ( !intQueue.isQueueEmpty() ) {
intQueue.dequeue( dequeueInteger );
cout << dequeueInteger << " dequeued" << endl;
intQueue.printQueue();
}
Queue< double > doubleQueue;
double val = 1.1, dequeuedouble;
cout << "processing a double Queue" << endl;
for ( i = 0; i < 4; i++ ) {
doubleQueue.enqueue( val );
doubleQueue.printQueue();
val += 1.1;
}
while ( !doubleQueue.isQueueEmpty() ) {
doubleQueue.dequeue( dequeuedouble );
cout << dequeuedouble << " dequeued" << endl;
doubleQueue.printQueue();
}
system("pause");
return 0;
}
// Fig. 15.3: list.h
// Template List class definition
#ifndef LIST_H
#define LIST_H
#include <iostream>
#include <cassert>
#include "listnd.h"
using std::cout;
template< class NODETYPE >
class List {
public:
List(); // constructor
~List(); // destructor
void insertAtFront( const NODETYPE & );
void insertAtBack( const NODETYPE & );
bool removeFromFront( NODETYPE & );
bool removeFromBack( NODETYPE & );
bool isEmpty() const;
void print() const;
private:
ListNode< NODETYPE > *firstPtr; // pointer to first node
ListNode< NODETYPE > *lastPtr; // pointer to last node
// Utility function to allocate a new node
ListNode< NODETYPE > *getNewNode( const NODETYPE & );
};
// Default constructor
template< class NODETYPE >
List< NODETYPE >::List() : firstPtr( 0 ), lastPtr( 0 ) { }
// Destructor
template< class NODETYPE >
List< NODETYPE >::~List()
{
if ( !isEmpty() ) { // List is not empty
cout << "Destroying nodes ...\n";
ListNode< NODETYPE > *currentPtr = firstPtr, *tempPtr;
while ( currentPtr != 0 ) { // delete remaining nodes
tempPtr = currentPtr;
cout << tempPtr->data << '\n';
currentPtr = currentPtr->nextPtr;
delete tempPtr;
}
}
cout << "All nodes destroyed\n\n";
}
// Insert a node at the front of the list
template< class NODETYPE >
void List< NODETYPE >::insertAtFront( const NODETYPE &value )
{
ListNode< NODETYPE > *newPtr = getNewNode( value );
if ( isEmpty() ) // List is empty
firstPtr = lastPtr = newPtr;
else { // List is not empty
newPtr->nextPtr = firstPtr;
firstPtr = newPtr;
}
}
// Insert a node at the back of the list
template< class NODETYPE >
void List< NODETYPE >::insertAtBack( const NODETYPE &value )
{
ListNode< NODETYPE > *newPtr = getNewNode( value );
if ( isEmpty() ) // List is empty
firstPtr = lastPtr = newPtr;
else { // List is not empty
lastPtr->nextPtr = newPtr;
lastPtr = newPtr;
}
}
// Delete a node from the front of the list
template< class NODETYPE >
bool List< NODETYPE >::removeFromFront( NODETYPE &value )
{
if ( isEmpty() ) // List is empty
return false; // delete unsuccessful
else {
ListNode< NODETYPE > *tempPtr = firstPtr;
if ( firstPtr == lastPtr )
firstPtr = lastPtr = 0;
else
firstPtr = firstPtr->nextPtr;
value = tempPtr->data; // data being removed
delete tempPtr;
return true; // delete successful
}
}
// Delete a node from the back of the list
template< class NODETYPE >
bool List< NODETYPE >::removeFromBack( NODETYPE &value )
{
if ( isEmpty() )
return false; // delete unsuccessful
else {
ListNode< NODETYPE > *tempPtr = lastPtr;
if ( firstPtr == lastPtr )
firstPtr = lastPtr = 0;
else {
ListNode< NODETYPE > *currentPtr = firstPtr;
while ( currentPtr->nextPtr != lastPtr )
currentPtr = currentPtr->nextPtr;
lastPtr = currentPtr;
currentPtr->nextPtr = 0;
}
value = tempPtr->data;
delete tempPtr;
return true; // delete successful
}
}
// Is the List empty?
template< class NODETYPE >
bool List< NODETYPE >::isEmpty() const
{ return firstPtr == 0; }
// Return a pointer to a newly allocated node
template< class NODETYPE >
ListNode< NODETYPE > *List< NODETYPE >::getNewNode(
const NODETYPE &value )
{
ListNode< NODETYPE > *ptr =
new ListNode< NODETYPE >( value );
assert( ptr != 0 );
return ptr;
}
// Display the contents of the List
template< class NODETYPE >
void List< NODETYPE >::print() const
{
if ( isEmpty() ) {
cout << "The list is empty\n\n";
return;
}
ListNode< NODETYPE > *currentPtr = firstPtr;
cout << "The list is: ";
while ( currentPtr != 0 ) {
cout << currentPtr->data << ' ';
currentPtr = currentPtr->nextPtr;
}
cout << "\n\n";
}
#endif
// Fig. 15.3: listnd.h
// ListNode template definition
#ifndef LISTND_H
#define LISTND_H
template< class NODETYPE > class List; // forward declaration
template<class NODETYPE>
class ListNode {
friend class List< NODETYPE >; // make List a friend
public:
ListNode( const NODETYPE & ); // constructor
NODETYPE getData() const; // return data in the node
private:
NODETYPE data; // data
ListNode< NODETYPE > *nextPtr; // next node in the list
};
// Constructor
template<class NODETYPE>
ListNode< NODETYPE >::ListNode( const NODETYPE &info )
: data( info ), nextPtr( 0 ) { }
// Return a copy of the data in the node
template< class NODETYPE >
NODETYPE ListNode< NODETYPE >::getData() const { return data; }
#endif
#ifndef QUEUE_H
#define QUEUE_H
#include "list.h"
template< class QUEUETYPE >
class Queue: private List< QUEUETYPE > {
public:
void enqueue( const QUEUETYPE &d ) { insertAtBack( d ); }
bool dequeue( QUEUETYPE &d )
{ return removeFromFront( d ); }
bool isQueueEmpty() const { return isEmpty(); }
void printQueue() const { print(); }
};
#endif |
|