首页 > 解决方案 > 在重新声明时添加默认参数使此构造函数成为默认构造函数

问题描述

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

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

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;
    }

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

标签: c++

解决方案


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

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;
}

推荐阅读