1. clang-tidy: Non-const lvalue reference to type 'basic_string<>' cannot bind to a temperary of type 'basic_string<>'
错误描述:
这个错误出现在我试图传一个临时变量给一个 函数,这个函数的参数类型为 引用(非常量引用)
原因分析:
根据编译错误提示可以知道,不能将形参begin、end绑定到a.begin()和a.end()的返回值,因为该返回值是一个临时量,临时量的生命周期可能在a.begin()和a.end()执行完后就结束了。因此编译器认为普通引用绑定一个临时量,在find_int函数中可能会修改这个临时量,然而此时临时量可能已经被销毁,从而导致一些未定义的行为,因此编译器不允许将普通引用绑定到一个临时量上。
解决办法:
在函数定义的地方加上const变成常引用或者去掉&引用。
这篇博客C++ non-const lvalue reference cannot bind to a temporary讲的很清楚。
参考:
C++之error: cannot bind non-const lvalue reference of type ‘myString&’ to an rvalue of type ‘myString’
C++ non-const lvalue reference cannot bind to a temporary
2. error: expected class-name before ‘{’ token
class xxxxx : public xxxxxxx{
问题描述: 出现这个问题是因为我错误的重复包含了头文件。 就是头文件 A.h 中包含 C.h, D.h, E.h 而在D.h 中又包含了A.h 导致编译器报错。
解决办法: 解决办法只要去掉重复包含的头文件就可以了。
补充: 出现这个问题还有可能是 定义类的时候 忘记在括号后面添加分号, 即 class A{} (漏掉了后面的分号, 正确的应该是 class A{}; )。
参考:
expected class-name before '{' token https://blog.csdn.net/cwj649956781/article/details/7953748