[c++新手问题] redefinition of default argument 报错应该如何解决 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
borischenc
V2EX    C

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

  •  1
     
  •   borischenc 2017-01-19 03:59:04 +08:00 3353 次点击
    这是一个创建于 3231 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 条附言    2017-01-19 14:39:27 +08:00
    问题已经解决了!

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

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

    #ifndef binary_tree_hpp
    #define binary_tree_hpp

    所以照理来说是不会出现重复包含的情况的。
    nifury
        20
    nifury  
       2017-01-19 14:28:36 +08:00
    @borischenc
    不,你在 binary_tree.cpp 里包含一次,在 main 里包含一次,所以重复了
    只要在一个 CPP 里定义,然后 hpp 里 extern 就行
    borischenc
        21
    borischenc  
    OP
       2017-01-19 14:39:07 +08:00
    @nifury 嗯嗯解决了!!谢谢!!
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1040 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 51ms UTC 18:33 PVG 02:33 LAX 10:33 JFK 13:33
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86