重载构造函数的C++调用是不明确的

grd*_*grd 6 c++ constructor default constructor-overloading c++11

假设我有这个虚拟类定义:

    class Node
    {
    public:
        Node ();
        Node (const int = 0);
        int getVal();
    private:
        int val;
    };
Run Code Online (Sandbox Code Playgroud)

虚拟构造函数实现仅用于教育目的:

Node::Node () : val(-1)
{
    cout << "Node:: DEFAULT CONSTRUCTOR" << endl;
}


Node::Node(const int v) : val(v) 
{
    cout << "Node:: CONV CONSTRUCTOR val=" << v << endl;
}    
Run Code Online (Sandbox Code Playgroud)

现在,如果我编译(使用选项-Wall -Weffc++ -std=c++11:),下面的代码:

#include <iostream>
#include "node.h"
using namespace std;

int main()
{   
    Node n;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误,根本不编译:

node_client.CPP: In function ‘int main()’:
node_client.CPP:10:16: error: call of overloaded ‘Node()’ is ambiguous
  Node n;  
                ^
node_client.CPP:10:16: note: candidates are:
In file included from node_client.CPP:4:0:
node.h:14:5: note: Node::Node(int)
     Node (const int = 0);     
     ^
node.h:13:2: note: Node::Node()
  Node ();
  ^
Run Code Online (Sandbox Code Playgroud)

我不明白为什么.

据我所知(我正在学习C++),因为具有不同的参数签名,所以调用不Node::Node()应该是模棱两可的Node::Node(const int).

有一些我想念的东西:它是什么?

πάν*_*ῥεῖ 9

对Node :: Node()的调用对于Node :: Node(const int)不应该是模糊的,因为它具有不同的参数签名.

当然这是模棱两可的.三思而后行!

你有

    Node ();
    Node (const int = 0);
Run Code Online (Sandbox Code Playgroud)

你打电话时应该选择哪一个Node()?具有默认值参数的那个?

它应该工作而不提供默认值:

    Node ();
    Node (const int); // <<<<<<<<<<<<< No default
Run Code Online (Sandbox Code Playgroud)