vs2005 error LNK2019: 无法解析的外部符号,该如何解决
vs2005 error LNK2019: 无法解析的外部符号
编译提示
test.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall BinaryTree<int>::pre_Order(void)" (?pre_Order@?$BinaryTree@H@@QAEXXZ),该符号在函数 _main 中被引用
1>test.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall BinaryTree<int>::insert(int)" (?insert@?$BinaryTree@H@@QAEXH@Z),该符号在函数 _main 中被引用
请问这是啥意思啊,为什么会出现这个错误啊?谢谢大家
------解决思路----------------------
模板的类的成员函数实现直接放在头文件中去,不要这样分离,不知道现在最新的编译器是否支持这样分离编译了
------解决思路----------------------
模板不支持分离编译,把CPP中实现部分也放到头文件中就可以了
为什么是这样请参看:http://blog.sina.com.cn/s/blog_46625a5f010000ld.html
// BinaryTree.h
#pragma once
#include <iostream>
using namespace std;
template<class T>
struct BinaryTreeNode{
T value;
BinaryTreeNode* p_Left;
BinaryTreeNode* p_Right;
BinaryTreeNode(T val){
value = val;
p_Left = NULL;
p_Right = NULL;
}
};
template<class T>
class BinaryTree{
public:
BinaryTree():p_Root(NULL),Length(0){}
void insert(T val);
void pre_Order();
void in_Order();
void back_order();
void BFS_Order();
private:
BinaryTreeNode<T> *p_Root;
int Length;
};
//BinaryTree.cpp
#pragma once
#ifndef BINARYTREE_H_
#define BINARYTREE_H_
#include <iostream>
#include <stdio.h>
#include <stack>
#include "BinaryTree.h"
using namespace std;
template<class T>
void BinaryTree<T>::insert(T val){
if(p_Root == NULL)
{
p_Root = new BinaryTreeNode<T>(val);
return;
}
BinaryTreeNode<T> *p_index = p_Root;
while(p_index != NULL)
{
if(p_index->value > val)
{
if(p_index->p_Left == NULL)
{
p_index->p_Left = new BinaryTreeNode<T>(val);
return;
}
p_index = p_index->p_Left;
}
if(p_index->value <= val)
{
if(p_index->p_Right == NULL)
{
p_index->p_Right = new BinaryTreeNode<T>(val);
return;
}
p_index = p_index->p_Right;
}
}
}
template<class T>
void BinaryTree<T>::pre_Order(){
if(p_Root == NULL)return;
stack<BinaryTreeNode<T> *> S;
S.push(NULL);
BinaryTreeNode<T> *p_index = p_Root;
while(p_index!=NULL||!S.empty()){
cout << p_index->value << ' ';
if(p_index->p_Right != NULL)S.push(p_index->p_Right);
if(p_index->p_Left != NULL)p_index = p_index->p_Left;
else
{
p_index = S.top();
S.pop();
}
}
return;
}
/*
void BinaryTree::in_Order();
void BinaryTree::back_order();
void BinaryTree::BFS_Order();
*/
#endif
//test.cpp
#ifndef BINARYTREE_H_
#define BINARYTREE_H_
#include <iostream>
#include "BinaryTree.h"
using namespace std;
int main(){
BinaryTree<int> BT;
int n,value;
cin >> n;
cin >> value;
for(int i = 0;i<n;++i)
{
cin >> value;
BT.insert(value);
}
BT.pre_Order();
return 0;
}
#endif
编译提示
test.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall BinaryTree<int>::pre_Order(void)" (?pre_Order@?$BinaryTree@H@@QAEXXZ),该符号在函数 _main 中被引用
1>test.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall BinaryTree<int>::insert(int)" (?insert@?$BinaryTree@H@@QAEXH@Z),该符号在函数 _main 中被引用
请问这是啥意思啊,为什么会出现这个错误啊?谢谢大家
------解决思路----------------------
模板的类的成员函数实现直接放在头文件中去,不要这样分离,不知道现在最新的编译器是否支持这样分离编译了
------解决思路----------------------
模板不支持分离编译,把CPP中实现部分也放到头文件中就可以了
为什么是这样请参看:http://blog.sina.com.cn/s/blog_46625a5f010000ld.html