[c++新手问题] redefinition of default argument 报错应该如何解决

2017-01-19 03:59:04 +08:00
 borischenc

header 的代码如下


#ifndef binary_tree_hpp
#define binary_tree_hpp

#include <iostream>
#include <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 上搜索了一番还是没有得到明确的答案。望各路大神指出问题~

谢谢!

2603 次点击
所在节点    C
21 条回复
Valyrian
2017-01-19 04:00:47 +08:00
extern 那行去掉等号后面的东西?
borischenc
2017-01-19 04:02:51 +08:00
@Valyrian 如果去掉的话在没有参数对函数进行调用的时候,如`print_tree();`则会显示 No matching function for call to 'private tree'
Valyrian
2017-01-19 04:06:38 +08:00
那再加一行 extern string print_tree(); 这样?
borischenc
2017-01-19 04:10:16 +08:00
@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)
MCVector
2017-01-19 04:30:30 +08:00
definition 里面不应该有默认参数值吧
Valyrian
2017-01-19 04:30:55 +08:00
查了一下,好像是你原版的基础上把 cpp 文件里的等号后面删掉
Sorrow
2017-01-19 04:34:19 +08:00
default argument 应该写在函数声明里,而不是函数定义里。
ipoh
2017-01-19 08:46:02 +08:00
>> 改成 > >
araraloren
2017-01-19 08:52:11 +08:00
默认参数只能出现在声明
定义中不能加
pagict
2017-01-19 08:55:32 +08:00
7L +1
snnn
2017-01-19 08:56:23 +08:00
@ipoh 新版本无所谓了
lazyhare
2017-01-19 09:32:20 +08:00
extern 后面有初始化就算是定义了, extern 失去意义了。再从其它地方定义 print_tree 当然就算 redefine 了
zonyitoo
2017-01-19 09:37:27 +08:00
7 楼和 9 楼说了答案。你在定义处不要写 default argument
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"
fgcmaster
2017-01-19 09:39:17 +08:00
实现的 cpp 不能加上默认参数,你把 cpp 文件中的默认参数删除掉看看,
borischenc
2017-01-19 13:50:44 +08:00
@zonyitoo
@lazyhare
@fgcmaster

谢谢大家的帮助,我现在把代码修改了一下,把 extern 关键字去掉了, default argument 也只在函数的头文件里面定义了。但是出现了另一个报错,说
borischenc
2017-01-19 13:52:05 +08:00
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 "";
}
nifury
2017-01-19 14:13:31 +08:00
@borischenc 你把 root 写在头文件里然后重复包含了吧。。
borischenc
2017-01-19 14:22:06 +08:00
@nifury 因为头文件里面有

#ifndef binary_tree_hpp
#define binary_tree_hpp

所以照理来说是不会出现重复包含的情况的。
nifury
2017-01-19 14:28:36 +08:00
@borischenc
不,你在 binary_tree.cpp 里包含一次,在 main 里包含一次,所以重复了
只要在一个 CPP 里定义,然后 hpp 里 extern 就行

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/335513

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX