首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

各位大大春节好,一个小疑点向大家请问一下

2012-04-21 
各位大大春节好,一个小问题向大家请教一下一个书上的习题,编译可以通过。。。。求助大家解决。我把代码贴出来:

各位大大春节好,一个小问题向大家请教一下
一个书上的习题,编译可以通过。。。。求助大家解决。

我把代码贴出来:

头文件:

#ifndef QUEUETP_H_
#define QUEUETP_H_
#include <string>
#include <iostream>

template <class Type>

class Queue
{
private:
//class scope definitions
//Node is a nested structure definition local to this class

struct Node { Type item;struct Node * next;};
enum {Q_SIZE =10};

//private class members
Node * front; //pointer to front of Queue
Node * rear; //pointer to rear Queue
int items; //current number of items in Queue
const int qsize; //maximum number of items in Queue

//preemptive definitions to prevent public copying
Queue (const Queue & q): qsize(0) {}
Queue & operator = (const Queue & q) {return *this;}

public:
Queue (int qs = Q_SIZE); //create queue with a qs limit
~Queue();
bool isempty() const;
bool isfull() const;
int queuecount() const;
bool enqueue (const Type &item); //add item to end
bool dequeue (Type &item); //remove item from front
};


class Worker
{
private:
std::string fullname;
long id;
public:
Worker():fullname("no one"),id(0) {}
Worker(const std::string & s,long n):fullname(s),id(n) {}
Worker(char * f,long n):fullname(f),id(n){}
std::string getfullname()const{return fullname;};
  double getid()const {return id;} ;
void Set() ;
void Show() const;
};


#endif



#include "QueueTp.h"
using namespace std;

//Queue methods
template <class Type>
Queue<Type>::Queue(int qs):qsize(qs)
{
front = rear = NULL;
items = 0;
}

template <class Type>
Queue<Type>::~Queue()
{
Node *temp;
while (front !=NULL) //while queue is not yet empty
{
temp = front //save address of front item
front = front->next //reset poniter to next item
delete temp; //delete former front
}
}

template <class Type>
bool Queue<Type>::isempty() const
{
return items==0;
}

template <class Type>
bool Queue<Type>::isfull() const
{
return items==qsize;
}

template <class Type>
int Queue<Type>::queuecount() const
{
return items;
}

//Add item to queue
template <class Type>
bool Queue<Type>::enqueue(const Type &item)
{
if(isfull())
return false;
Node * add = new Node; //create new node
if(add ==NULL)
return false //quit if none available
add->item = item;
add->next=NULL;
items++
if(front ==NULL) //if queue is empty
front = add; //place item at front
else
rear->next = add; //else place at rear
rear = add; //have rear point to new node
return true;
}

//Place front item into item variable and remove from queue
template <class Type>
bool Queue<Type>::dequeue(Type &item)
{
if (front ==NULL)
return false;
item = front->item; //set item to first item in queue
items--;
Node *temp = front; //save location of first item
front = front->next; //rest front to next item
delete temp;
if ( items ==0)
rear ==NULL;
return true;
}

//Worker methods





void Worker::Set()
{
cout<<"Enter worker's name: ";
getline(cin, fullname);
cout<<"Enter worker's ID:";
cin>>id;
while(cin.get() !='\n')
continue;
}

void Worker::Show() const


{
cout<<"Worker's fullname: "<<fullname<<endl;
cout<<"Worker's id: "<<id<<endl;
}


执行代码:

#include <iostream>
#include "QueueTp.h"
#include <string>

const int SIZE = 5;
using namespace std;

int main()
{
/*Queue<Worker *> pt;
Worker temp1[2],*temp2;
for(int i =0;i<2;i++)
{
temp1[i].Set();
pt.enqueue(&temp1[i]);
}

for(int i =0;i<2;i++)
{
pt.dequeue(temp2);
temp2->Show();
}*/

Queue<Worker>*pt=new Queue<Worker>(2);
  Worker temp1,temp2;
  temp2.Set();
  if(!pt->enqueue(temp2))
  {
cout<<"en queue is full failed";
return 0;
  }
  pt->dequeue(temp1);
  temp1.Show();


  return 0; 
}


这是出错提示,我搞不清楚,dequeue和enqueue两个函数调用错在哪里了
1>------ 已启动生成: 项目: 14.7-3, 配置: Debug Win32 ------
1>正在编译...
1>QueueTp.cpp
1>正在链接...
1>14.7-3.obj : error LNK2019: 无法解析的外部符号 "public: bool __thiscall Queue<class Worker>::dequeue(class Worker &)" (?dequeue@?$Queue@VWorker@@@@QAE_NAAVWorker@@@Z),该符号在函数 _main 中被引用
1>14.7-3.obj : error LNK2019: 无法解析的外部符号 "public: bool __thiscall Queue<class Worker>::enqueue(class Worker const &)" (?enqueue@?$Queue@VWorker@@@@QAE_NABVWorker@@@Z),该符号在函数 _main 中被引用
1>14.7-3.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Queue<class Worker>::Queue<class Worker>(int)" (??0?$Queue@VWorker@@@@QAE@H@Z),该符号在函数 _main 中被引用
1>D:\robbi program\first fighting\c++plus\cap14\14.7-3\Debug\14.7-3.exe : fatal error LNK1120: 3 个无法解析的外部命令
1>生成日志保存在“file://d:\robbi program\first fighting\c++plus\cap14\14.7-3\14.7-3\Debug\BuildLog.htm”
1>14.7-3 - 4 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========



[解决办法]
模板类的实现也必须放在头文件中,不能放cpp,这和普通类不同

热点排行