在我当前的项目中,我正在使用模板创建一个二进制搜索树。我将所有函数都工作在binarySearchTree.h文件中(首先我对所有内容进行了内联),但是当我去将某些函数定义放入binarySearchTree.cpp文件中时(取消了大部分.h文件的内联) Clion似乎找不到.cpp文件。我总结出这是问题所在,因为错误与在.cpp文件中没有找到任何函数的定义有关,但是除了在其中包括.cpp文件之外,我无法弄清楚该要做的事情。我的主要语言,而不是.h,似乎编码不好。
如上所述,我已经将代码作为h文件分别进行了测试,并且所有内容都内联了,这是可行的。问题出现后,我检查了我的CMakeLists.txt文件,以确保我的.cpp文件包含在可执行文件中,并且确实包含在其中。最后,我尝试在我的main.cpp文件中包括binarySearchTree.cpp,而不是binarySearchTree.h(我很确定我不应该在上交的最终项目中)只是为了查看它是否有效,并且确实。因此,我假设CLion链接.h和.cpp文件的方式出了问题。
CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(BSTProject)
set(CMAKE_CXX_STANDARD 14)
add_executable(BSTProject main.cpp binarySearchTree.cpp binarySearchTree.h)
binarySearchTree.cpp
#include "binarySearchTree.h"
using namespace std;
template <class DataType>
void binarySearchTree<DataType>::eraseNode(treeNode<DataType> *deleteNode) {…}
template <class DataType>
bool binarySearchTree<DataType>::erase(const DataType &deleteItem) {…}
template <class DataType>
bool binarySearchTree<DataType>::find(const DataType &searchItem,void (*foundNode)(const DataType &)) {…}
template <class DataType>
void binarySearchTree<DataType>::traverseNodes(treeNode<DataType> *node,void (**itemFound)(const DataType &)) const {…}
template <class DataType>
void binarySearchTree<DataType>::insert(const DataType &newItem) {…}
binarySearchTree.h
#ifndef BSTPROJECT_BINARYSEARCHTREE_H
#define BSTPROJECT_BINARYSEARCHTREE_H
#include <cstddef>
template<class DataType>
class binarySearchTree;
// treeNode class
template<class DataType>
class treeNode {…};
// binarySearchTree class
template<class DataType>
class binarySearchTree {
private:
std::size_t treeSize;
treeNode<DataType> *root;
void eraseNode(treeNode<DataType> *deleteNode);
public:
binarySearchTree() {
treeSize = 0;
root = nullptr;
}
bool empty() const {
return treeSize == 0;
}
std::size_t size() const {
return treeSize;
}
bool find(const DataType &searchItem,void (*foundNode)(const DataType &));
void traverse(void (*itemFound)(const DataType &foundItem)) const {…}
void traverseNodes(treeNode<DataType> *node,void (**itemFound)(const DataType &foundItem)) const;
void debug() {…}
bool erase(const DataType &deleteItem);
void insert(const DataType &newItem);
// Your binarySearchTree goes here
};
template<class DataType>
static void debugPrint(const DataType &item) {…}
#endif //BSTPROJECT_BINARYSEARCHTREE_H
main.cpp
#include <iostream>
#include <string>
#include "binarySearchTree.h"
using namespace std;
int main() {
binarySearchTree<int> tree;
tree.insert(2);
tree.insert(3);
tree.insert(1);
tree.debug();
tree.erase(2);
tree.debug();
return 0;
}
它在构建中失败,但是输出最终应该是: 1个 2 3 1个 3 错误是main.cpp.obj:错误LNK2019:无法解析的外部符号,后面跟有问题的函数名称(下面是完整的错误消息)
[ 33%] Building CXX object CMakeFiles/BSTProject.dir/binarySearchTree.cpp.obj
binarySearchTree.cpp
[ 66%] Linking CXX executable BSTProject.exe
LINK Pass 1: command
"C:\PROGRA~2\micrOS~1\2019\COMMUN~1\VC\Tools\MSVC\1422~1.279\bin\Hostx86\x86\link.exe /nologo @CMakeFiles\BSTProject.dir\objects1.rsp /out:BSTProject.exe /implib:BSTProject.lib /pdb:C:\Users\varin\CLionProjects\BSTProject\cmake-build-debug\BSTProject.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\BSTProject.dir/intermediate.manifest CMakeFiles\BSTProject.dir/manifest.res" failed (exit code 1120) with the following output:
main.cpp.obj : error LNK2019: unresolved external symbol "public: void __thiscall binarySearchTree<int>::traverseNodes(class treeNode<int> *,void (__cdecl**)(int const &))const " (?traverseNodes@?$binarySearchTree@H@@QBEXPAV?$treeNode@H@@PAP6AXABH@Z@Z) referenced in function "public: void __thiscall binarySearchTree<int>::traverse(void (__cdecl*)(int const &))const " (?traverse@?$binarySearchTree@H@@QBEXP6AXABH@Z@Z)
main.cpp.obj : error LNK2019: unresolved external symbol "public: bool __thiscall binarySearchTree<int>::erase(int const &)" (?erase@?$binarySearchTree@H@@QAE_NABH@Z) referenced in function _main
main.cpp.obj : error LNK2019: unresolved external symbol "public: void __thiscall binarySearchTree<int>::insert(int const &)" (?insert@?$binarySearchTree@H@@QAEXABH@Z) referenced in function _main
BSTProject.exe : fatal error LNK1120: 3 unresolved externals