
header 的代码如下
#ifndef binary_tree_hpp #define binary_tree_hpp #include <iostream> #include &t;string> #include <stack> #include <vector> using namespace std; struct Node{ Node * left; Node * right; int degree; int key; int value; }; Node * root; Node* add(Node *); Node* add(int, int); extern string print_tree(stack<Node*, vector<Node*>> = stack<Node*, vector<Node*>>(*new vector<Node*>(1,root))); #endif /* binary_tree_hpp */ 出问题的在 external 这一行,在 cpp 文件中是这样的
string print_tree(stack<Node*, vector<Node*>> nodes = stack<Node*, vector<Node*>>(*new vector<Node*>(1,root))){ cout<<nodes.top()<<endl<<root<<endl; return ""; } 编译的时候就提示 redefinition of default argument 。
不知应该如何解决,在 google 上搜索了一番还是没有得到明确的答案。望各路大神指出问题~
谢谢!
1 Valyrian 2017-01-19 04:00:47 +08:00 via iPhone extern 那行去掉等号后面的东西? |
2 borischenc OP @Valyrian 如果去掉的话在没有参数对函数进行调用的时候,如`print_tree();`则会显示 No matching function for call to 'private tree' |
3 Valyrian 2017-01-19 04:06:38 +08:00 via iPhone 那再加一行 extern string print_tree(); 这样? |
4 borischenc OP @Valyrian 还是会报错, Undefined symbols for architecture x86_64: "print_tree()", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) |
5 MCVector 2017-01-19 04:30:30 +08:00 via Android definition 里面不应该有默认参数值吧 |
6 Valyrian 2017-01-19 04:30:55 +08:00 via iPone 查了一下,好像是你原版的基础上把 cpp 文件里的等号后面删掉 |
7 Sorrow 2017-01-19 04:34:19 +08:00 via iPad default argument 应该写在函数声明里,而不是函数定义里。 |
8 ipoh 2017-01-19 08:46:02 +08:00 via Android >> 改成 > > |
9 araraloren 2017-01-19 08:52:11 +08:00 默认参数只能出现在声明 定义中不能加 |
10 pagict 2017-01-19 08:55:32 +08:00 7L +1 |
12 lazyhare 2017-01-19 09:32:20 +08:00 extern 后面有初始化就算是定义了, extern 失去意义了。再从其它地方定义 print_tree 当然就算 redefine 了 |
13 zonyitoo 2017-01-19 09:37:27 +08:00 7 楼和 9 楼说了答案。你在定义处不要写 default argument |
14 lazyhare 2017-01-19 09:38:12 +08:00 顺手给你加个引用"The distinction between a declaration and a definition may seem obscure at this point but is actually important. To use a variable in more than one file requires declarations that are separate from the variable ’ s definition. To use the same variable in multiple files, we must define that variable in one and only one file. Other files that use that variable must declare but not define that variable." example: "extern double pi = 3.1416; // definition" 详见<C++ Primer 5th> "2.2.2. Variable Declarations and Definitions" |
15 fgcmaster 2017-01-19 09:39:17 +08:00 实现的 cpp 不能加上默认参数,你把 cpp 文件中的默认参数删除掉看看, |
16 borischenc OP |
17 borischenc OP duplicate symbol _root in: /Users/Boris/Library/Developer/Xcode/DerivedData/Algorithems_ToolBox-duheuacdjorkbdensozspjevuypq/Build/Intermediates/Algorithems ToolBox.build/Debug/Algorithems ToolBox.build/Objects-normal/x86_64/binary_tree.o /Users/Boris/Library/Developer/Xcode/DerivedData/Algorithems_ToolBox-duheuacdjorkbdensozspjevuypq/Build/Intermediates/Algorithems ToolBox.build/Debug/Algorithems ToolBox.build/Objects-normal/x86_64/main.o 这个是 main.cpp 的代码: #include <iostream> #include "binary_tree.hpp" int main(){ print_tree(); } 这个是 binary_tree.hpp 的代码,我觉得并没有重复申明 root 变量啊,只在头文件里面生成了。 #include "binary_tree.hpp" using namespace std; Node* get_min(Node* node){ if (node->left) return get_min(node->left); return node; } Node* add(int key, int value){ Node* node; node->key = key; node->value = value; return add(node); } Node* add(Node * node){ Node* current_node = root; while(1){ if (!current_node) { current_node = node; return current_node; } if (node->key == current_node->key) { return current_node; } else if (node->key > current_node->key){ current_node = current_node->right; } else{ current_node = current_node-> left; } } } string print_tree(stack<Node*, vector<Node*>> nodes){ cout<<nodes.top()<<endl<<root<<endl; return ""; } |
18 nifury 2017-01-19 14:13:31 +08:00 @borischenc 你把 root 写在头文件里然后重复包含了吧。。 |
19 borischenc OP |
20 nifury 2017-01-19 14:28:36 +08:00 |
21 borischenc OP @nifury 嗯嗯解决了!!谢谢!! |