C++刷题错误集锦

本文解析了C++编程中常见的错误,包括堆缓存溢出、数组访问越界、无效初始化、类型转换失败等问题,并提供了详细的解决方案及注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. AddressSanitizer: heap-buffer-overflow on address
    堆缓存移除,数组访问越界了。
  2. C++之invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
    函数原型上参数是int类型,但是在调用函数的时候却是“int&”(int的地引用类型)。
    哪怕函数原型中
    bool findNumberIn2DArray(vector<vector>& matrix, int target) ;
    参数列表中有vector<vector>& matrix这个&,但是他在这里不是地址引用的意思,只是说这个参数是直接对原对象进行更改,而不是只是拷贝一个副本,传值。
    所以主函数中调用时,应是
	cout << findNumberIn2DArray(matrix,target);

而不是

	cout << findNumberIn2DArray(&matrix,target);
  1. could not convert ‘s.std::set<_Key, _Compare, _Alloc>::find<int, std::less, std::allocator >(((const key_type)it.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator*<int*, std::vector >()))’ from ‘std::set::iterator {aka std::_Rb_tree_const_iterator}’ to ‘bool’
set<int> s = {1,2};
s

set :: find()函数是预定义的函数,用于检查元素是否属于集合,如果元素在集合容器中找到,则返回指向该元素的迭代器。(所以判定)

  1. cannot bind ‘std::ostream {aka std::basic_ostream}’ lvalue to ‘std::basic_ostream&&’
    原因:直接将vector对象通过cout输出。 vector v = {1,2,3}; cout << v;
  2. 力扣报错:error: non-void function does not return a value in all control paths [-Werror,-Wreturn-type]
    原因:not all control paths return a value 这句话的意思是函数并不是所有分支都有返回值。
    就是函数中有些else if的分支情况没有返回值,需要增返回值
  3. c++expected member name or ‘;’ after declaration specifiers for(int & num:nums) {
    **原因:**上面代码中报错的for循环,目的是对result数组进行初始化,但这个for循环不是一个函数,在类里面是不允许这种执行语句的,必须转换成函数才行。我自己把for循环写出了函数外了
  4. runtime error: addition of unsigned offset to 0x602000000090 overflowed to 0x60200000008c
    **原因:**数组越界
  5. AddressSanitizer: heap-buffer-overflow on address
    原因:堆溢出,for循环条件没写对进入死循环。
  6. Id returned 1exit status 和 undefined reference to `WinMain’
    C++文件中没有写main函数
  7. 运行程序时提示错误:
    terminate called after throwing an instance of ‘std::logic_error’ what(): basic_string::_S_construct null not valid
    原因:自己给string类型变量赋了一个int类型的值,string构造不能传入空指针xxx,即在xxx为空时会报上面错误,保证func返回不为空即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值