2012-10-21 85 views
0

可能重复:
What is an undefined reference/unresolved external symbol error and how do I fix it?C++:错误LNK2019:解析外部符号

所以我比较我的问题已经从这一类衍生本网站的问题,广大图书馆。我仍然不知道为什么我的编译器在抱怨。

因此,让我简单介绍一下我想用我的程序做什么。我正在研究电梯算法。我想要使​​用二进制堆实现优先级队列。

我已经从马克艾伦维斯网站的源代码。对于堆。

我正在构建我自己的代表二叉树节点的reqnode类。

reqnode =请求节点。

该方案是:用户提出请求。我服务请求。我正在处理多个请求。因此,我必须使用首先满足哪个用户的优先级。

这是我的二进制堆的头文件。

#include <vector> 
using namespace std; 

BinaryHeap class. 

CONSTRUCTION: with no parameters or vector containing items. 

template <class Comparable> 
class BinaryHeap 
{ 
public: 
BinaryHeap(); 
BinaryHeap(const vector<int> & v); 

bool isEmpty() const; 
const Comparable & findMin() const; 

void insert(const Comparable & x); 
void deleteMin(); 
void deleteMin(Comparable & minItem); 
void makeEmpty(); 

private: 
int theSize; // Number of elements in heap 
vector<Comparable> Array; // The heap Array 

void buildHeap(); 
void percolateDown(int hole); 
}; 

这些是为我的二进制堆定义的函数。

#include "binaryHeap.h" 
using namespace std; 

// Construct the binary heap. 
template <class Comparable> 
BinaryHeap<Comparable>::BinaryHeap() 
: Array(11), theSize(0) 
{ 
} 

// Insert item x into the priority queue, maintaining heap order. 
// Duplicates are allowed. 


template <class Comparable> 
void BinaryHeap<Comparable>::insert(const Comparable & x) 
{ 
Array[ 0 ] = x; // initialize sentinel 
if(theSize + 1 == Array.size()) 
    Array.resize(Array.size() * 2 + 1); 

    // Percolate up 
int hole = ++theSize; 
for(; x < Array[ hole/2 ]; hole /= 2) 
    Array[ hole ] = Array[ hole/2 ]; 
Array[ hole ] = x; 
} 

// Find the smallest item in the priority queue. 
// Return the smallest item, or throw UnderflowException if empty. 
template <class Comparable> 
const Comparable & BinaryHeap<Comparable>::findMin() const 
{ 
if(isEmpty()){ 
    cout << "heap empty" << endl; //throw UnderflowException(); 
    break; 
} 
return Array[ 1 ]; 
} 

// Remove the smallest item from the priority queue. 
// Throw UnderflowException if empty. 
template <class Comparable> 
void BinaryHeap<Comparable>::deleteMin() 
{ 
if(isEmpty()){ 
    cout << "heap empty" << endl; //throw UnderflowException(); 
    break; 
} 

Array[ 1 ] = Array[ theSize-- ]; 
percolateDown(1); 
} 

// Remove the smallest item from the priority queue 
// and place it in minItem. Throw UnderflowException if empty. 
template <class Comparable> 
void BinaryHeap<Comparable>::deleteMin(Comparable & minItem) 
{ 
minItem = findMin(); 
Array[ 1 ] = Array[ theSize-- ]; 
percolateDown(1); 
} 

// Establish heap-order property from an arbitrary 
// arrangement of items. Runs in linear time. 
template <class Comparable> 
void BinaryHeap<Comparable>::buildHeap() 
{ 
for(int i = theSize/2; i > 0; i--) 
    percolateDown(i); 
} 

// Test if the priority queue is logically empty. 
// Return true if empty, false otherwise. 
template <class Comparable> 
bool BinaryHeap<Comparable>::isEmpty() const 
{ 
return theSize == 0; 
} 

// Make the priority queue logically empty. 
template <class Comparable> 
void BinaryHeap<Comparable>::makeEmpty() 
{ 
theSize = 0; 
} 

// Internal method to percolate down in the heap. 
// hole is the index at which the percolate begins. 
template <class Comparable> 
void BinaryHeap<Comparable>::percolateDown(int hole) 
{ 
int child; 
Comparable tmp = Array[ hole ]; 

for(; hole * 2 <= theSize; hole = child) 
{ 
    child = hole * 2; 
    if(child != theSize && Array[ child + 1 ] < Array[ child ]) 
     child++; 
    if(Array[ child ] < tmp) 
     Array[ hole ] = Array[ child ]; 
    else 
     break; 
} 
Array[ hole ] = tmp; 
} 

这是为了reQnode

class reqNode//create a node that takes in several properties. 
{ 
public: 
reqNode(){ //default constructor 
static priority = start = destination = timestamp = start_time = finish_time = -1; 
} 

reqNode(const reqNode &copy){  //copy constructor 
priority = copy.priority; 
start = copy.start; 
destination = copy.destination; 
timestamp = copy.timestamp; 
start_time = copy.start_time; 
finish_time = copy.finish_time; 
} 

reqNode & operator=(const reqNode & copy){ 
priority = copy.priority; 
start = copy.start; 
destination = copy.destination; 
timestamp = copy.timestamp; 
start_time = copy.start_time; 
finish_time = copy.finish_time; 

return *this; 
} 

int priority, start, destination, timestamp, start_time, finish_time; 
bool direction; 

bool operator<(reqNode &rhs){ 
if(this->priority < rhs.priority) 
return true; 
else 
return false; 
} 

void setPriority(int x){ 
priority=x; 
} 
}; 

我的头文件,这是我的驱动程序实现

#include <iostream> 
#include <fstream> 
#include <string> 
#include "binaryHeap.h" 
#include "reqnode.h" 

#include <algorithm> 
using namespace std; 

void setNode(reqNode nizzode, int priority) 
{ 
nizzode.priority = priority; 
} 


int main() 
{ 
int numItems = 10000; 
BinaryHeap<reqNode> h; 
int i = 37; 
reqNode x; 
reqNode * temp; 

for(i = 37; i != 0; i = (i + 37) % numItems){ 
temp = new reqNode; 
temp->setPriority(i); 
h.insert(*temp); 
} 

for(i = 1; i < numItems; i++) 
{ 
h.deleteMin(x); 
if(x.priority != i) 
cout << "Oops! " << i << endl; 
} 

for(i = 37; i != 0; i = (i + 37) % numItems){ 
temp = new reqNode; 
temp->setPriority(i); 
h.insert(*temp); 
} 

temp = new reqNode; 
temp->setPriority(i); 
h.insert(*temp); 


return 0; 
} 

最后,这些都是困扰着我的错误!

1>Driver2.obj : error LNK2019: unresolved external symbol "public: void __thiscall BinaryHeap<class reqNode>::deleteMin(class reqNode &)" ([email protected][email protected]@@@@[email protected]@@Z) referenced in function _main 
1>Driver2.obj : error LNK2019: unresolved external symbol "public: void __thiscall BinaryHeap<class reqNode>::insert(class reqNode const &)" ([email protected][email protected]@@@@[email protected]@@Z) referenced in function _main 
1>Driver2.obj : error LNK2019: unresolved external symbol "public: __thiscall BinaryHeap<class reqNode>::BinaryHeap<class reqNode>(void)" ([email protected]@@@@[email protected]) referenced in function _main 
1>C:\Users\Aaron Artis\Documents\Visual Studio 2010\Projects\Elevator_Algo_Remix1\Debug\Elevator_Algo_Remix1.exe : fatal error LNK1120: 3 unresolved externals 

我不确定在哪里查明这个问题。我有一个以前的链接器错误。我解决了它。这个看起来像是一个愚蠢的人。

+0

您的模板方法是在.h或.cpp中定义的吗?通常模板成员在与模板类声明相同的文件中定义。 – alestanis

回答

2

不要将模板定义放在.CPP文件中 - 它们需要位于.H文件中,否则编译器无法扩展它们。

也许,你甚至不应该需要BinaryHeap.c

+0

我认为这个事实让许多人第一次编写模板代码时感到震惊。 – john

+0

@john从这个问题(和答案)的重复次数来看,这个问题在SO上非常流行,你的评估当然是有效的。 – WhozCraig

0

它看起来像构造函数的定义,这两种方法是在一个CPP文件;他们需要在一个头文件中。

1

你的编译器是抱怨,因为它发现声明的功能构造,deleteMininsert,但不定义

这是由于您的定义必须位于.cpp文件中。

在类声明之后,将它们直接放入binaryHeap.h文件中,可以解决问题。

想想这样做的模板功能。

+0

不一定总是。你知道专门的模板吗? –

+0

是的,我。我只是强调了这样一个事实,即这不仅仅是为了解决他的实际错误,而是他在处理模板时应该考虑的事情。 – alestanis

+0

当然,但“想到”和“总是这样做”是完全不同的。当你制定一个规则时,确保覆盖角落的情况,或者至少不要让它听起来那么严格。 –

0

简答:你应该把你的BinaryHead<>的声明和定义放在同一个文件中,因为这是一个模板类。

Long答案:阅读this Stackoverflow answer,其中详细解释了事情。

相关问题