标识符 "buildTree" 未定义
Identifier "buildTree" is undefined
有一个简单的错误。查看了一堆帖子并检查了我文件中的原因,但似乎无法修复。从我的 BST.cpp 文件调用的函数在标题中抛出错误(“标识符“buildTree”未定义)。有问题的函数是 'buildTree' 和 'performSearchBST'。CPP 文件是包含在 main 中,class 模板在所有函数中声明。可能是一个简单的错误,但希望有人能帮我指出。下面列出的代码。一如既往地提前感谢您的帮助。
主文件:
//..
#include "BST.cpp"
#include "Node.h"
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main()
{
//...
ifstream filename("grocery_upc_database.csv");
BST<UPC> tree = buildTree(filename); //build binary search tree of UPC objects
string code;
cout << "Please enter a UPC code(! to quit): ";
cin >> code;
while (code != "!")
{
long entry = stol(code); //convert user inputted string to type long int
UPC key(entry);
performSearchBST(tree, key);
cout << "\nPlease enter a UPC code(! to quit): ";
cin >> code;
}
return 0;
}
BST.cpp
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <fstream>
#include "BST.h"
using namespace std;
template <class T>
BST<T>::BST()
{
root = NULL;
}
template <class T>
BST<T>::BST(long upcdata, string descrip)
{
root->data = upcdata;
root->descrip = descrip;
}
template <class T>
void BST<T>::buildTree(string inFile)
{
string upcholder;
string description;
string line;
ifstream file;
file.open("grocery_upc_database.csv");
while (getline(file, line))
{
stringstream ss(line);
getline(ss, upcholder, ',');
getline(ss, description, '\t');
Node<T> newNode;
newNode->data = stol(upcholder);
newNode->descrip == description;
insert(newNode);
}
}
template <class T>
void BST<T>::insert(Node<T> *newNode)
{
insert2(root, newNode);
}
template <class T>
void insert2(Node<T> *&root, Node<T> *newNode)
{
if (root->data = NULL)
{
root = newNode;
}
else if (newNode->data < root->data)
{
insert2(root->left, newNode);
}
else
{
insert(root->right, newNode);
}
}
template <class T>
long BST<T>::stol(string item)
{
long i = atol(item.c_str());
return i;
}
template <class T>
bool BST<T>::performSearchBST(BST<T> bst, UPC key)
{
return performSearchBST2(bst,key);
}
template <class T>
bool performSearchBST2(BST<T> bst, UPC key)
{
if (bst.root == NULL)
return false;
if (bst.root->data == key)
return true;
else if (root->data < key)
return performSearchBST2(root->right, key);
else
return performSearchBST2(root->left, key);
}
BST.h
#include <iostream>
#include <string>
#include "Node.h"
using namespace std;
template <class T>
class BST
{
Node<T> *root;
public:
BST();
BST(long key, string descrip);
bool find(T item);
bool performSearchBST(BST<T> bst, UPC key);
void buildTree(string inFile);
void insert(Node<T>* newNode);
long stol(string item);
};
你这里有一个简单的错误。
首先,您的编译器不知道什么是buildTree(String)
。
您有一个 non-static 成员函数,其名称为 BST<T>::buildTree(String)
.
其次,如评论中所述,您不能在没有该类型对象的情况下使用此类非静态成员函数,因为它对特定对象进行操作。
您可以通过:
将其用作成员函数,因此:
BST<UPC> tree{};
tree.buildTree(filename);
或者,将函数转换为构建树的静态函数,它应该如下所示:
static BST<T> buildTree(string inFile)
{
\* Create and build the tree inside
return the tree
*/
}
然后您可以通过以下方式使用它:
BST<UPC> tree = BST<UPC>::buildTree(filename);
函数的正确名称。
此外,使用模板时,您需要在同一个 .hpp 文件中声明和定义函数 see this question for more information。
有一个简单的错误。查看了一堆帖子并检查了我文件中的原因,但似乎无法修复。从我的 BST.cpp 文件调用的函数在标题中抛出错误(“标识符“buildTree”未定义)。有问题的函数是 'buildTree' 和 'performSearchBST'。CPP 文件是包含在 main 中,class 模板在所有函数中声明。可能是一个简单的错误,但希望有人能帮我指出。下面列出的代码。一如既往地提前感谢您的帮助。
主文件:
//..
#include "BST.cpp"
#include "Node.h"
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main()
{
//...
ifstream filename("grocery_upc_database.csv");
BST<UPC> tree = buildTree(filename); //build binary search tree of UPC objects
string code;
cout << "Please enter a UPC code(! to quit): ";
cin >> code;
while (code != "!")
{
long entry = stol(code); //convert user inputted string to type long int
UPC key(entry);
performSearchBST(tree, key);
cout << "\nPlease enter a UPC code(! to quit): ";
cin >> code;
}
return 0;
}
BST.cpp
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <fstream>
#include "BST.h"
using namespace std;
template <class T>
BST<T>::BST()
{
root = NULL;
}
template <class T>
BST<T>::BST(long upcdata, string descrip)
{
root->data = upcdata;
root->descrip = descrip;
}
template <class T>
void BST<T>::buildTree(string inFile)
{
string upcholder;
string description;
string line;
ifstream file;
file.open("grocery_upc_database.csv");
while (getline(file, line))
{
stringstream ss(line);
getline(ss, upcholder, ',');
getline(ss, description, '\t');
Node<T> newNode;
newNode->data = stol(upcholder);
newNode->descrip == description;
insert(newNode);
}
}
template <class T>
void BST<T>::insert(Node<T> *newNode)
{
insert2(root, newNode);
}
template <class T>
void insert2(Node<T> *&root, Node<T> *newNode)
{
if (root->data = NULL)
{
root = newNode;
}
else if (newNode->data < root->data)
{
insert2(root->left, newNode);
}
else
{
insert(root->right, newNode);
}
}
template <class T>
long BST<T>::stol(string item)
{
long i = atol(item.c_str());
return i;
}
template <class T>
bool BST<T>::performSearchBST(BST<T> bst, UPC key)
{
return performSearchBST2(bst,key);
}
template <class T>
bool performSearchBST2(BST<T> bst, UPC key)
{
if (bst.root == NULL)
return false;
if (bst.root->data == key)
return true;
else if (root->data < key)
return performSearchBST2(root->right, key);
else
return performSearchBST2(root->left, key);
}
BST.h
#include <iostream>
#include <string>
#include "Node.h"
using namespace std;
template <class T>
class BST
{
Node<T> *root;
public:
BST();
BST(long key, string descrip);
bool find(T item);
bool performSearchBST(BST<T> bst, UPC key);
void buildTree(string inFile);
void insert(Node<T>* newNode);
long stol(string item);
};
你这里有一个简单的错误。
首先,您的编译器不知道什么是buildTree(String)
。
您有一个 non-static 成员函数,其名称为 BST<T>::buildTree(String)
.
其次,如评论中所述,您不能在没有该类型对象的情况下使用此类非静态成员函数,因为它对特定对象进行操作。
您可以通过:
将其用作成员函数,因此:
BST<UPC> tree{};
tree.buildTree(filename);
或者,将函数转换为构建树的静态函数,它应该如下所示:
static BST<T> buildTree(string inFile)
{
\* Create and build the tree inside
return the tree
*/
}
然后您可以通过以下方式使用它:
BST<UPC> tree = BST<UPC>::buildTree(filename);
函数的正确名称。
此外,使用模板时,您需要在同一个 .hpp 文件中声明和定义函数 see this question for more information。