关于用链表来实现栈和队列功能的有关问题
关于用链表来实现栈和队列功能的问题。
我想知道为什么我的主函数文件里不能调用那些功能,求大神啊。
错误 1 error LNK2019: 无法解析的外部符号 "public: __thiscall Stack<int>::Stack<int>(void)" (??0?$Stack@H@@QAE@XZ),该符号在函数 _main 中被引用 D:\C++\TEST\TEST\TEST\StackQueue.obj TEST
错误 2 error LNK2019: 无法解析的外部符号 "public: void __thiscall Stack<int>::push(int)" (?push@?$Stack@H@@QAEXH@Z),该符号在函数 _main 中被引用 D:\C++\TEST\TEST\TEST\StackQueue.obj TEST
错误 3 error LNK2019: 无法解析的外部符号 "public: int __thiscall Stack<int>::pop(void)" (?pop@?$Stack@H@@QAEHXZ),该符号在函数 _main 中被引用 D:\C++\TEST\TEST\TEST\StackQueue.obj TEST
错误 4 error LNK2019: 无法解析的外部符号 "public: int __thiscall Stack<int>::top(void)" (?top@?$Stack@H@@QAEHXZ),该符号在函数 _main 中被引用 D:\C++\TEST\TEST\TEST\StackQueue.obj TEST
错误 5 error LNK2019: 无法解析的外部符号 "public: __thiscall Queue<int>::Queue<int>(void)" (??0?$Queue@H@@QAE@XZ),该符号在函数 _main 中被引用 D:\C++\TEST\TEST\TEST\StackQueue.obj TEST
错误 6 error LNK2019: 无法解析的外部符号 "public: void __thiscall Queue<int>::EnQueue(int)" (?EnQueue@?$Queue@H@@QAEXH@Z),该符号在函数 _main 中被引用 D:\C++\TEST\TEST\TEST\StackQueue.obj TEST
错误 7 error LNK2019: 无法解析的外部符号 "public: int __thiscall Queue<int>::DeQueue(void)" (?DeQueue@?$Queue@H@@QAEHXZ),该符号在函数 _main 中被引用 D:\C++\TEST\TEST\TEST\StackQueue.obj TEST
错误 8 error LNK1120: 7 个无法解析的外部命令 D:\C++\TEST\TEST\Debug\TEST.exe TEST
能不能帮忙解决下啊,谢谢各位大大了
------解决思路----------------------
把模板类的实现直接放到头文件中,不要像一般类一样放到.cpp中。好像还有一种分离编译方式,不过不推荐
我想知道为什么我的主函数文件里不能调用那些功能,求大神啊。
template <class T>
struct qNODE
{
qNODE<T>* next;
T data;
};
template <class T>
class Queue
{
public:
Queue();
void EnQueue(T e);
T DeQueue();
T front_element();
T rear_element();
//释放空间函数暂时没有加入。
private:
qNODE<T>* front; //指向头结点的指针。 front->next->data是队头第一个元素。
qNODE<T>* rear;//指向队尾(最后添加的一个元素)的指针
};
#include "Queue.h"
template <class T> Queue<T>::Queue()
{
qNODE<T>* p = new qNODE<T>;
if (NULL == p)
{
cout << "Error:Failed to get the node." << endl;
}
p->data = NULL;
p->next = NULL;
front = p;
rear = p;
}
template <class T> void Queue<T>::EnQueue(T e)
{
//在队尾入队
qNODE<T>* p = new qNODE<T>;
if (NULL == p)
{
cout << "Error:Failed to get the node." << endl;
}
p->data = e;
p->next = NULL;
rear->next = p;
rear = p;
}
template <class T> T Queue<T>::DeQueue()
{
T e;
if (front == rear)
{
cout << "The queue is empty." << endl;
return NULL;
}
else
{
qNODE<T>* p = front->next;
front->next = p->next;
e = p->data;
//当只有一个元素且删除它之后,rear指向的node没有了
//所以要指头结点
if (rear == p)
{
rear = front;
}
delete p;
p = NULL;
return e;
}
}
template <class T> T Queue<T>::front_element()
{
if (front == rear)
{
cout << "The queue is empty." << endl;
return NULL;
}
else
{
qNODE<T>* p = front->next;
return p->data;
}
}
template <class T> T Queue<T>::rear_element()
{
if (front == rear)
{
cout << "The queue is empty." << endl;
return NULL;
}
else
{
return rear->data;
}
}
template <class T>
struct sNODE
{
sNODE<T>* next;
T data;
};
template <class T>
class Stack
{
public:
Stack();
void push(T e) ;
T pop();
T top();
//释放空间函数暂时没有加入。
private:
sNODE<T>* shead;
};
#include "Stack.h"
template <class T> void Stack<T>::push(T e)
{
sNODE<T>* p = new sNODE<T>;
if (p == NULL)
{
cout << "Error:Failed to get a new node. " << endl;
}
else
{
p->data = e;
p->next = shead->next;
shead->next = p;
}
}
template <class T> Stack<T>::Stack()
{
shead = new sNODE<T>;
if (shead == NULL)
{
cout << "Error:Failed to get a new node. " << endl;
}
else
{
shead->data = NULL;
shead->next = NULL;
}
}
template <class T> T Stack<T>::pop()
{
T e;
sNODE<T>* p = shead->next;
if(p != NULL)
{
phead->next = p->next;
e = p->data;
delete p;
return e;
}
else
{
cout << "There is no elements in the stack." << endl;
return NULL;
}
}
template <class T> T Stack<T>::top()
{
T e;
sNODE<T>* p = shead->next;
if (p != NULL)
{
e = p->data;
return e;
}
else
{
cout << "There is no elements in the stack." << endl;
return NULL;
}
}
#include"Queue.h"
#include"Stack.h"
#include<iostream>
using namespace std;
const int CAP=5;
int main()
{
int x;
Stack<int> S[CAP];
Queue<int> Q[CAP];
for(int i=0;i<CAP;i++)
{
cin>>x;
S[i].push(x);
}
for(int i=0;i<CAP;i++)
{
cout<<"Stack: "<<S[i].top()<<endl;
}
cout<<"Than reverse all the elements.";
for(int i=0;i<CAP;i++)
{
Q[i].EnQueue(S[i].pop());
}
for(int i=0;i<CAP;i++)
{
S[i].push(Q[i].DeQueue());
}
for(int i=0;i<CAP;i++)
{
cout<<"The new stack: "<<S[i].top()<<endl;
}
}
错误 1 error LNK2019: 无法解析的外部符号 "public: __thiscall Stack<int>::Stack<int>(void)" (??0?$Stack@H@@QAE@XZ),该符号在函数 _main 中被引用 D:\C++\TEST\TEST\TEST\StackQueue.obj TEST
错误 2 error LNK2019: 无法解析的外部符号 "public: void __thiscall Stack<int>::push(int)" (?push@?$Stack@H@@QAEXH@Z),该符号在函数 _main 中被引用 D:\C++\TEST\TEST\TEST\StackQueue.obj TEST
错误 3 error LNK2019: 无法解析的外部符号 "public: int __thiscall Stack<int>::pop(void)" (?pop@?$Stack@H@@QAEHXZ),该符号在函数 _main 中被引用 D:\C++\TEST\TEST\TEST\StackQueue.obj TEST
错误 4 error LNK2019: 无法解析的外部符号 "public: int __thiscall Stack<int>::top(void)" (?top@?$Stack@H@@QAEHXZ),该符号在函数 _main 中被引用 D:\C++\TEST\TEST\TEST\StackQueue.obj TEST
错误 5 error LNK2019: 无法解析的外部符号 "public: __thiscall Queue<int>::Queue<int>(void)" (??0?$Queue@H@@QAE@XZ),该符号在函数 _main 中被引用 D:\C++\TEST\TEST\TEST\StackQueue.obj TEST
错误 6 error LNK2019: 无法解析的外部符号 "public: void __thiscall Queue<int>::EnQueue(int)" (?EnQueue@?$Queue@H@@QAEXH@Z),该符号在函数 _main 中被引用 D:\C++\TEST\TEST\TEST\StackQueue.obj TEST
错误 7 error LNK2019: 无法解析的外部符号 "public: int __thiscall Queue<int>::DeQueue(void)" (?DeQueue@?$Queue@H@@QAEHXZ),该符号在函数 _main 中被引用 D:\C++\TEST\TEST\TEST\StackQueue.obj TEST
错误 8 error LNK1120: 7 个无法解析的外部命令 D:\C++\TEST\TEST\Debug\TEST.exe TEST
能不能帮忙解决下啊,谢谢各位大大了
------解决思路----------------------
把模板类的实现直接放到头文件中,不要像一般类一样放到.cpp中。好像还有一种分离编译方式,不过不推荐