初入职常见问题:window程序移植到linux时,error: no matching function for call to 'std::exception:exception(const ch

将这一段里的这句话

        error: no matching function for call to 'std::exception:exception(const char[16])'

改成

        std::logic_error ex("XXXXX");
        throw std::exception(ex);

        还要带上头文件#include <stdexcept> 

 

std::logic_error只是其中一种,stdexcept定义的异常类还有很多,可以参考https://blog.youkuaiyun.com/fengbingchun/article/details/78303734

<think>我们遇到了一个C++编译错误:`error: no matching function for call to ‘Fireword::Fireword(double, const double&)’` 这个错误表示在尝试调用`Fireword`类的构造函数,没有找到与参数列表`(double, const double&)`匹配的构造函数。 ### 错误原因分析 1. **构造函数不存在**:`Fireword`类可能没有定义接受两个`double`类型参数的构造函数。 2. **参数类型不匹配**:虽然看起来是`double`和`const double&`,但可能实际需要的参数类型不同(例如`int`或自定义类型)。 3. **构造函数被声明为`explicit`**:如果构造函数被声明为`explicit`,则不能用于隐式转换,可能导致调用失败。 4. **构造函数访问权限问题**:构造函数可能是`private`或`protected`,在外部无法访问。 ### 解决方案 1. **检查构造函数声明**:确认`Fireword`类中是否定义了如下构造函数: ```cpp class Fireword { public: Fireword(double arg1, const double& arg2); // 或者类似形式 }; ``` 2. **添加缺失的构造函数**:如果确实需要这样的构造函数,但尚未定义,则添加它: ```cpp Fireword::Fireword(double arg1, const double& arg2) { // 实现代码 } ``` 3. **调整参数类型**:如果构造函数存在但参数类型不匹配,考虑: - 修改调用处的参数类型 - 或重载构造函数以支持当前参数类型 4. **检查`explicit`关键字**:如果构造函数被声明为`explicit`,则需要显式调用: ```cpp Fireword obj = Fireword(3.14, 2.718); // 显式构造 ``` 5. **检查访问权限**:确保构造函数在`public`区域声明。 ### 示例修正 假设原始类定义缺少对应构造函数: ```cpp class Fireword { public: // 添加双参数构造函数 Fireword(double a, const double& b); }; // 构造函数实现 Fireword::Fireword(double a, const double& b) { // 初始化操作 } ``` ### 常见错误场景 - **传递了错误的参数数量**:检查是否传递了两个参数,而不是更多或更少。 - **参数类型不完全匹配**:例如,构造函数需要`int`和`double`,但传递了两个`double`。 ### 调试技巧 1. 使用`g++ -E`查看预处理后的代码,确认函数调用位置。 2. 检查类定义的头文件是否被正确包含。 3. 检查是否有命名空间限定问题(如`Fireword`在某个命名空间中)。 §§相关问题§§ 1. C++中构造函数声明为`explicit`的作用是什么? 2. 如何为类定义多个不同参数类型的构造函数(重载构造函数)? 3. 当出现"no matching function"错误,除了参数类型外还需要检查哪些因素? 4. 在C++11中,委托构造函数如何帮助减少重复代码? 5. 为什么有候即使构造函数存在,编译器仍报告"no matching function"(考虑模板实例化问题)?</think>### C++编译错误解析:`no matching function for call to ‘Fireword::Fireword(double, const double&)’` #### 错误原因分析 这个错误表示在创建`Fireword`类对象,**编译器找不到匹配的构造函数**。常见原因包括: 1. **缺少对应的构造函数** ```cpp class Fireword { public: // 缺少接收两个double参数的构造函数 Fireword(); // 默认构造函数 Fireword(int a); // 只接收int的构造函数 }; // 尝试调用 Fireword obj(3.14, 2.718); // 错误:没有匹配的构造函数 ``` 2. **参数类型不匹配** ```cpp class Fireword { public: Fireword(float x, float y); // 参数是float类型 }; Fire
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值