记录敲c++代码曾遇到的错误

 

 

一、字符串操作

1、使用空的char*指针构造std::string。

        出现运行时错误:terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct NULL not valid。

        std::string str = pszStr;//赋值前需要检查pszStr是否为空

2、类型定义

typedef unsigned char BYTE;

这句代码编译时提示错误如下:

longpackage.h:18: error: duplicate ‘unsigned’
longpackage.h:18: error: multiple types in one declaration
longpackage.h:18: error: declaration does not declare anything

查证后原因为:BYTE类型重复。

解决方法: 1、使用#ifndef检查后再定义类型。

2、使用#undef BYTE取消已有定义。


3、在写一个namespace内代码时,编译出现错误:error C2143: syntax error : missing ';' before 'namespace'。

定义如下:

#include "ref_count_t.h"
#include <string.h>
namespace bf_com_util //编译提示这一行出错
{

xxxxx函数定义

}

排查发现:和第一个include头文件有关。

在ref_count.h中定义了一个类(无论是普通类还是模板类都一样)

class Test
{
....//无论任何函数都不影响
};//问题位置

类定义最后的分号";"决定了一个编译错误。如果没有分号就提示上面的编译错误。(原因待补充)


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用[1]中的错误信息"terminate called after throwing an instance of 'std::length_error' what(): basic_string::_M_create"是由于在使用std::stoi函数将字符串转换为整数时,字符串的长度超过了整数类型的范围所导致的。这个错误通常发生在将一个过大的字符串转换为整数时。 引用中的错误信息"terminate called after throwing an instance of 'std::runtime_error' what():..."是由于在使用ROS Comm进行编译时,可能出现了运行时错误。这个错误可能是由于环境变量设置不正确或者其他编译相关的问题导致的。 "terminate called after throwing an instance of 'std::invalid_argument' what(): stoi"错误是由于在使用std::stoi函数将字符串转换为整数时,字符串的格式不正确所导致的。这个错误通常发生在字符串无法正确解析为整数时,比如字符串中包含了非数字字符。 以下是一个演示如何处理这个错误的例子: ```cpp #include <iostream> #include <stdexcept> int main() { std::string str = "abc"; try { int num = std::stoi(str); std::cout << "Converted number: " << num << std::endl; } catch (const std::invalid_argument& e) { std::cerr << "Invalid argument error: " << e.what() << std::endl; } return 0; } ``` 这个例子中,我们将一个非数字字符串"abc"尝试转换为整数。由于字符串无法正确解析为整数,所以会抛出std::invalid_argument异常。在catch块中,我们捕获并处理了这个异常,并输出了错误信息"Invalid argument error: stoi"。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值