【发布时间】:2022-12-03 16:50:35
【问题描述】:
我坚持如何将项目分成几个源文件和头文件。我目前的做法似乎是笨拙和错误的。任何评论表示赞赏!
我有四个文件:
-
main.cpp是主程序。它将创建一些树节点,并调用一个函数来遍历它们。 -
TreeNode.h是我声明一个简单类TreeNode的头文件 -
TreeNode.cpp是我定义类TreeNode的构造函数的地方 -
utils.cpp是我在TreeNode上定义一些函数的地方,比如打印树。
问题是,我应该把includeTreeNode.h文件放在哪里?
- 如果我将它同时包含在
main.cpp和utils.cpp中(因为它们都使用TreeNode类,我的编译器会给我一个“重复符号”错误。这可能是因为我在@987654334 中包含了utils.cpp@还有。
像这样 :
Scanning dependencies of target main
[ 25%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[ 50%] Building CXX object CMakeFiles/main.dir/utils.cpp.o
[ 75%] Linking CXX executable main
duplicate symbol __Z13inorder_printP8TreeNode in:
CMakeFiles/main.dir/main.cpp.o
CMakeFiles/main.dir/utils.cpp.o
duplicate symbol __Z16inorderTraversalP8TreeNode in:
CMakeFiles/main.dir/main.cpp.o
CMakeFiles/main.dir/utils.cpp.o
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [main] Error 1
make[2]: *** [CMakeFiles/main.dir/all] Error 2
make[1]: *** [CMakeFiles/main.dir/rule] Error 2
make: *** [main] Error 2`
- 如果我只在
main.cpp文件中包含TreeNode.h,utils.cpp文件将不会编译。它给出了一个错误error: unknown type name 'TreeNode'
编辑:
下面是四个文件:
主.cpp
#include <iostream>
#include <vector>
#include "TreeNode.h"
#include "utils.cpp"
using namespace std;
int main() {
TreeNode * root = new TreeNode(0);
root->right = new TreeNode(2);
root->right->right = new TreeNode(3);
// inorder_print(root);
std::vector<int> v = inorderTraversal(root);
// print out vector
for (auto i = v.begin(); i != v.end(); ++i){
std::cout << *i << ' ';
}
std::cout << std::endl;
return 0;
}
树节点.h
#ifndef TREE_TREE_H
#define TREE_TREE_H
class TreeNode{
public:
int val;
TreeNode * left;
TreeNode * right;
TreeNode(int x);
};
#endif //TREE_TREE_H
树节点.cpp
#include "TreeNode.h"
TreeNode::TreeNode(int x) {
val = x;
left = nullptr;
right = nullptr;
}
实用程序.cpp
#include <vector>
#include <iostream>
// #include "TreeNode.h"
// tested correct
void inorder_print(TreeNode * root){
// print out the tree content in inorder traversal
while(root != nullptr){
std::cout << root->val << std::endl;
inorder_print(root->left);
inorder_print(root->right);
break;
}
}
std::vector<int> inorderTraversal(TreeNode * root){
std::vector<int> v;
while(root != NULL){
v.push_back(root->val);
if (root->left != NULL){
v.insert(v.end(), inorderTraversal(root->left).begin(), inorderTraversal(root->left).end());
break;
}
if (root->right != NULL){
v.insert(v.end(), inorderTraversal(root->right).begin(), inorderTraversal(root->right).end());
break;
}
break;
}
return v;
}
【问题讨论】:
-
你用过头球后卫吗?您不在另一个文件中包含 cpp 文件。您为其创建另一个头文件。
-
@MichaelSurette 不,我以前没听说过。但谢谢你给我介绍它!
-
您应该在任何需要它的源文件中包含
TreeNode.h文件。您还应该将其包含在这个问题中,因为它是您遇到的问题的核心。 -
你能至少提供每个文件的包含部分吗?
-
@PeteBecker 感谢您提供检查。现在发布。
标签: c++ header-files software-design