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;
}
我不是 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;
}
推荐阅读
- stored-procedures - 如何在 Blazor 服务器应用程序中简单地执行存储过程?
- java - The method orElseThrow(() -> {}) is undefined for the type Employee
- linux - 启动大量后台作业,但将它们的日志分开
- node.js - Nextjs 的两个错误:res.json 不是函数并且 getserversideprops 没有返回对象
- java - 使用 Spring Webflux 对响应应用分页是否会阻塞?
- python-3.x - 事件中的条目未更新列表
- java - 休眠更新后休眠延迟加载导致json序列化异常
- json - Microsoft Azure 数据工厂(突触分析):链接服务 || 响应代码错误 400:来自服务器的未知错误
- delphi - 如何仅捕获屏幕上的鼠标单击区域?
- react-native - 离线输入数据并自动调用api更新数据到数据库