在重新声明时添加默认参数使此构造函数成为默认构造函数

Ton*_*oni 4 c++

它说在重新声明时添加默认参数使这个构造函数成为默认构造函数。

我对它做了一些研究,但我只是不明白我需要做什么来解决这个问题。

struct Transaction{
    int type;
    int amount;
    int to_from_type;
    Transaction(int, int, int);
};

Transaction :: Transaction(int type=0, int amount=0, int etc=0)
{
    this->type=type;
    this->amount=amount;
    this->to_from_type=etc;
}




 Transaction :: Transaction(int type=0, int amount=0, int etc=0) //I am getting an error at this code and I cannot fix it.

    {
        this->type=type;
        this->amount=amount;
        this->to_from_type=etc;
    }
Run Code Online (Sandbox Code Playgroud)

我不是 C++ 方面的专家,很想得到一些关于我的代码的帮助。

Fai*_*ash 8

XCode 结合使用 CLang 和 Apple LLVM 来编译您的 C++ 代码。Clang 强制执行一些额外的检查,其中包括您的案例。这里发生的事情是您定义了一个构造函数来接受 3 个参数,但是您的实现可以在没有任何参数的情况下被调用,这意味着您实现的一个实际上带有与隐式默认构造函数相同的方法签名(方法名称和参数列表),而带有 3 params(在结构中定义的那个)在编译器的眼中被遗漏了。修复很简单:

struct Transaction{
    int type;
    int amount;
    int to_from_type;
    Transaction(int=0, int=0, int=0);
};

Transaction :: Transaction(int type, int amount, int etc)
{
    this->type=type;
    this->amount=amount;
    this->to_from_type=etc;
}
Run Code Online (Sandbox Code Playgroud)