2012-08-27 110 views
0

我正在尝试为类项目创建一个队列数组。我已经为FIFO队列编写了规范文件和实现文件。然而,当我写:如何创建一个队列数组?

QueType<movieType> fifoQueue[6]; 

我收到此错误信息:

错误LNK2019:无法解析的外部符号 “公用:__thiscall QueType :: QueType(无效)”?(?? 0 $ QueType @VmovieType @@@@ QAE @ XZ)在函数_main中引用

为了能够创建队列数组,我需要添加什么?另外,我不能使用STL队列。

这里是主文件:

#include "movieType.h" 
#include <iostream> 
#include <fstream> 
#include "QueType.h" 

using namespace std; 

int main() 
{ 
movieType movie[9]; 
ifstream inFile("movie1.txt"); 

    //I get the error here 
    QueType<movieType> fifoQueue[6]; 

    return 0; 
} 

这里是规范文件:

//definition of NodeType 
template <class ItemType> 
struct NodeType 
{ 
ItemType info; // store data 
NodeType* next; // sotre location of data 
}; 

//exception class used when queue is full 
class FullQueue 
{}; 

//Exception class used when queue is empty 
class EmptyQueue 
{}; 

//templated queue class 
template<class ItemType> 
class QueType 
{ 
public: 
    QueType(); 
    //Function: class constructor 
    //Precondition: none 
    //Postcondition: it initializes the pointers, front and rear to null 

    ~QueType(); 
    //Function:class destructor 
    //Precondition: queue has been initialized 
    //Postcondition: deallocate allocated memory 

    void MakeEmpty(); 
    //Function: determines whether the queue is empty 
    //Precondition: queue has been initialized 
    //Postcondition:queue is empty 

    bool IsEmpty() const; 
    //Function:determines whether the queue is empty 
    //Precondition:queue has been initialized 
    //Postcondition:Function value = (queue is empty) 

    bool IsFull() const; 
    //Function:determines whether the queue is full 
    //Precondition:queue has been initialized 
    //Postcondition:Function value = (queue is full) 

    void Enqueue(ItemType newItem); 
    //Function:Adds newItem to the rear of the queue 
    //Precondition:queue has been initialized 
    //Postcondition:if (queue is full), FullQueue exception is thrown, 
    //else newItem is at rear of queue 

    void Dequeue(ItemType& item); 
    //Function:removes front item from the queue and returns it in item 
    //Precondition:queue has been initialized 
    //Postcondition:if (queue is empty), EmptyQueue exception is thrown 
    //and item is undefines, else front element has been removed from 
    //queue and item is a copy of removed element 

private: 
    NodeType<ItemType>* front; //pointer points to the front to the queue 
    NodeType<ItemType>* rear; // pointer points to the rear of the queue 
}; 

和规范文件:

#include "QueType.h"//gives access to QueType class 
#include <cstddef> //for NULL 
#include <new> // for bad_alloc 

template<class ItemType> 
QueType<ItemType>::QueType() 
{ 
front = NULL; 
rear = NULL; 
} 

template <class ItemType> 
QueType<ItemType>::~QueType() 
{ 
MakeEmpty(); 
} 

template <class ItemType> 
void QueType<ItemType>::MakeEmpty() 
{ 
NodeType<ItemType>* tempPtr;//temporary pointer 

while(front != NULL) 
{ 
    tempPtr=front; 
    front = front->next; 
    delete tempPtr; 
} 
rear = NULL; 
} 

template <class ItemType> 
bool QueType<ItemType>::IsEmpty() const 
{ 
return (front == NULL); 
} 

template <class ItemType> 
bool QueType<ItemType>::IsFull() const 
{ 
NodeType<ItemType>* location; 
try 
{ 
    location = new NodeType<ItemType> 
    delete location; 
    return false; 
} 
catch(std::bad_alloc exception) 
{ 
    return true; 
} 
} 

template <class ItemType> 
void QueType<ItemType>::Enqueue(ItemType newItem) 
{ 
if (IsFull()) 
     throw FullQueue(); 
else 
{ 
    NodeType<ItemType>* newNode; 
    newNode = new NodeType<ItemType>; 
    newNode ->info=newItem; 
    newNode->next=NULL; 
    if(rear== NULL) 
      front= newNode; 
    else 
      rear->next=newNode; 
    rear=newNode; 
} 
} 

回答

1

您需要实现中声明的功能你的“规格文件”。

此特定错误消息只抱怨了QueType构造丢失的实现(因为这是你正在使用的mainQueType的唯一功能),但只要你开始使用任何的其他的功能,你会得到类似的链接器错误。

在这种情况下,因为您的类是模板化的,您需要将您的定义移动到头文件中,以便编译器在创建专业化时访问它们。

欲了解更多信息,请参阅this question

+0

我已经添加了实现文件。 – user1561949

+0

@ user1561949答复已更新。 – Fraser

+0

感谢您的帮助! – user1561949