我正在试图找出为什么我的代码没有编译,这表示未解析的外部。我试图弄清楚问题是什么以及为什么它无法编译。
实际错误:
错误LNK2001:未解析的外部符号“public:virtual bool __thiscall BST,class std :: allocator> > :: isThere(类 的std :: basic_string的,类 std :: allocator>)“ (?isThere @?$ BST @ V'$ basic_string的@ DU?$ char_traits @ d @ @@ STD V'$分配器@ d @ @@ 2 STD @@@@ UAE_NV?$ basic_string的@ DU?$ char_traits @ d @ STD @@ V'$ @分配器@ d @@ 2 STD @@@ Z) 1> c:\ users \ jordin \ documents \ visual studio 2012 \ Projects \ ConsoleApplication16 \ Debug \ ConsoleApplication16.exe: 致命错误LNK1120:1个未解析的外部
主:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include "BST.h"
SearchableADT<string> *dictionary = new BST<string>;
int main()
{
char choice = 'z';
string tempdata,
fileloc;
cout<<"Welcome to Jordin Ray's Spell Check Application\n"
<<"Please pick a tree type\n"
<<"'b' -Binary Search Tree\n"
<<"'a' -AVL tree\n"
<<"'h' -hash table\n";
cin>>choice;
cout<<"Please give the exact location of the file for download\n";
cin>>fileloc;
if(choice == 'b')
{
dictionary->loadFromFile(fileloc);
char ch = 'z';
while(ch != 'q')
{
string word = "";
if(ch == 'e')
{
cout<<"Please enter the word you would like to search for\n";
cin>>word;
dictionary->isThere(word);
}
else if (ch == 'p')
{
cout<<"Please enter the partial word you would like to look for with a '?' where the missing letter is: \n"
<<"Ex. - '?nd' will search for words in the dictionary that have those last two letters\n";
// dictionary->partialIsThere(word);
}
else if (ch == 'c')
{
//dictionary.clear();
}
else if (ch == 's')
{
int entries;
entries = dictionary->numEntries();
cout<<"\nThe amount of entries logged in the database is: "<<entries<<".\n";
}
cout<<"Would you like to:\n"
<<"Clear the dictionary - 'c'\n"
<<"Check for an entry - 'e'\n"
<<"Check for a partial entry - 'p'\n"
<<"Report Statistics - 's'\n"
<<"Quit - 'q'";
cin>>ch;
}//end of while loop
}
return 0;
}//end of main()
BST.h
#ifndef BST_H
#define BST_H
#pragma once
#include "SearchableADT.h"
#include <cstring>
using namespace std;
template <typename T>
class BST : public SearchableADT<T>
{
private:
struct t_node
{
string data;
t_node *L;
t_node *R;
};
int numnodes;
t_node* head;
t_node* cPTR; //current pointer
t_node* pPTR; //parent pointer
t_node* tPTR; //temporary pointer
public:
BST(void);
~BST(void);
int loadFromFile(string filename);
//void clear(void);
void insertEntry(T info);
void deleteEntry(T info);
bool isThere(T info);
int numEntries(void);
//needed for comparison to AVL
int BST<T>::height(t_node* tPTR);
}; // end of class BST
//constructor
template <typename T>
BST<T>::BST(void)
{
head = NULL;
numnodes = 0;
}
//destructor
template <typename T>
BST<T>::~BST(void)
{
this->clear();
}
template <typename T>
void BST<T>::insertEntry(T info)
{
t_node* t = new t_node;
t->data = info;
t->L = NULL;
t->R = NULL;
pPTR = NULL;
if(head == NULL)
head = t;
else
{
t_node* cPTR; //current pointer
cPTR = head;
//checks to see if the data just entered is
//greater than the head
while(cPTR)
{
pPTR = cPTR;
if( t->data > cPTR->data)
cPTR = cPTR->R;
else
cPTR = cPTR->L;
}
//checks to see if the data just entered is
// less than the parent data that was
// set equal to cPTR;
if(t->data < pPTR->data)
{
pPTR->L = t;
}
else
{
pPTR->R = t;
}
}
} //end of insertEntry()
template <typename T>
void BST<T>::deleteEntry(T info)
{
//i use this as an example of abstraction
//after using this function the cPTR and pPTR are already pointing to the data needed to be found
if(!isThere(info))
{
cout<<"The data: '"<<info<<"' could not be found in the ADT, sorry!\n";
return;
}
//one has to account for all possibilities when deleting data
//1.) - Removing just a leaf node (no children) **easy
//2.) - Removing a leaf node with 1 child **medium
//3.) - Removing a leaf node with 2 children **hard
//case 1
if( cPTR->L == NULL && cPTR ->R == NULL)
{
if(pPTR-> L == cPTR)
pPTR->L = NULL;
else
pPTR->R = NULL;
delete cPTR;
return;
}//end of case 1
//case 2
else if((cPTR->L == NULL && cPTR->R != NULL) ||
(cPTR->L != NULL && cPTR->R == NULL))
{
//if the left data of cptr has data and the right data is NULL
if(cPTR->L != NULL && cPTR->R == NULL)
{
if(pPTR->L == cPTR)
{
pPTR->L = cPTR->R;
delete cPTR;
}
else
{
pPTR->R = cPTR->L;
delete cPTR;
}
}
else //right child present, and left child is NULL
{
if(pPTR->L == cPTR)
{
pPTR->L = cPTR->R;
delete cPTR;
}
else
{
pPTR->R = cPTR->R;
delete cPTR;
}
}
//case 3
if((cPTR->L != NULL && cPTR->R != NULL))
{
tPTR=cPTR->R;
//if the right node doesn't have a right leaf or left leaf, delete that node
if((tPTR->L == NULL && tPTR->R == NULL))
{
cPTR = tPTR;
delete cPTR;
cPTR->R = NULL;
}
else
{
if(tPTR->L != NULL)
{
t_node* secPTR;
secPTR = tPTR->L;
//cycle through left nodes until you find the lowest left node
while(secPTR->L != NULL)
{
secPTR = secPTR->L;
}
//because you're only setting the data equal to eachother the cPTR keeps its left and right nodes
//therefore it correctly replaces the data without unwanted loss of other information
cPTR->data = secPTR->data;
delete secPTR;
cPTR->L = NULL;
}
else
{
cPTR->data = tPTR->data;
cPTR->R = tPTR->R;
delete tPTR;
}
}
}
}
}//end of deleteEntry() function
template <typename T>
int BST<T>::numEntries(void)
{
cout << "Tree Height: " << height(head) << endl;
return numnodes;
}
template <typename T>
int BST<T>::loadFromFile(string filename)
{
int count = 0;
string tempdata;
ifstream fin(filename);
if(!fin)
{
cout<< "Error: Could no open file\n";
count--;
}
while(fin)
{
fin>>tempdata;
if(fin)
{
insertEntry(tempdata);
count++;
}
}
fin.close();
return count;
}//end of loadFromFile() function
template <typename T>
int BST<T>::height(t_node* tPTR)
{
if (head == NULL)
return -1;
else
return 1 + max(height(tPTR->L), height(tPTR->R));
tPTR=head;
}
#endif
SearchableADT.h
#ifndef SEARCHABLEADT_H
#define SEARCHABLEADT_H
#include <string>
using namespace std;
template <typename T>
class SearchableADT
{
public:
virtual int loadFromFile(string filename) = 0;
//virtual void clear(void) = 0;
virtual void insertEntry(T value) = 0;
virtual void deleteEntry(T value) = 0;
virtual bool isThere(T value) = 0 ;
virtual int numEntries(void) = 0;
};
#endif
答案 0 :(得分:0)
您需要定义BST::isThere( T info )
功能。