C++中印象深刻的编译错误(持续更新)
编译错误1: missing default argument on parameter
思路:一开始以为是函数的定义和声明中参数的数量于类型不一致,导致错误,但是反复修改验证还是报这个错误。
后来以为是函数调用处没有修改,导致错误,但是全部改完之后还是报这个错误。
最后google,查到:https://stackoverflow.com/questions/5740296/default-argument-missing-for-parameter-compiler-error
Parameters with default values have to come at the end of the list because, when calling the function, you can leave arguments off the end, but can't miss them out in the middle. Since your arguments have different types, you can get the same effect using an overload: void func ( string word, int b ) { // some jobs } void func ( int b ) { func("hello", b); }
You can't have non-default parameters after your default parameters begin. Put another way, how would you specify a value for b leaving word to the default of "hello" ? The arguments with a default value have to come in the end of the argument list. So just change your function declaration to void func(int b, string word = "hello")
意思是说拥有default值的参数必须放在最后,不然就会发生该错误。
后来我去查了一下,报错的位置的参数前面的确是有default值的参数,而我新加的参数是没有default值的。
解决办法:为了不调整调用顺序,我给我新加的参数也加入了default值。编译通过
编译错误2:non-virtual member function marked 'override' hides virtual member functions
这个例子中涉及到虚函数和override关键字
例子:
class A { public: virtual void add(); virtual void sub(); }; class B : public A { public: void abb(); void sub(); };
上面的代码可以通过编译,完全没问题,但是类B中的是abb(), 而A中是add(), 类B并没有重写类A中的add()函数,编译器认为是类B自己实现的函数,但是程序员却以为自己重写了,没有意识到自己写错,override修饰符可以保证编译器做一些检查,避免犯错。
如果派生类在虚函数声明时使用了override描述符,那么该函数必须重写其基类中的同名函数
override使用
class A { public: virtual void add(); virtual void sub() const; void print(); }; class B : public A { public: void abb() override; //拼写错误 only virtual member functions can be marked 'override' void add() override; void sub() override; // 常量性不一致 non-virtual member function marked 'override' hides virtual member function void sub() const override; };
我遇到的情况是:在函数后添加override关键字, 如果与基类的虚函数不一致,发生报错,如黄色强调的部分。
编译错误2参考链接:
- 解决编译问题:https://blog.csdn.net/qq_43657810/article/details/113915261
- 虚函数介绍:https://www.jb51.net/article/224129.htm
编译错误3:error: cannot take the address of an rvalue of type 'void *'
xxxxx/xxxxxx.cpp:344:12: error: cannot take the address of an rvalue of type 'void *' 344 | ppData = &(dmem->get_gfx_device_memory().get_m_handle_pmem()->get_m_handle().fd_va); | ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1 error generated. 09:31:08 ninja failed with: exit status 1
这个编译错误提示的是你不能对一个右值(rvalue)取地址。在 C++ 中,右值通常指的是一个临时对象或者是一个表达式的结果,它们的生命周期通常很短暂,因此不允许直接取它们的地址。
在你的代码中,dmem->get_gfx_device_memory().get_m_handle_pmem()->get_m_handle().fd_va
这一串调用链很可能产生了一个右值。具体来说,fd_va
很可能是一个成员变量,但它被嵌套在多个方法调用返回的临时对象内部。当你尝试通过 &
操作符获取这个成员变量的地址时,由于整个表达式的结果是一个右值,所以编译器报错。
为了解决这个问题,你可以尝试以下几种方法:
-
修改设计:如果可能的话,修改相关类的设计,使得你需要访问的成员变量或方法返回的对象不是临时的。这可能涉及到在类中添加一些新的成员函数或变量,以便直接访问所需的数据。
-
使用引用或指针:如果
get_m_handle_pmem()
和get_m_handle()
可以返回引用或指针而不是临时对象,那么这个问题就可以被解决。但这需要修改这些函数的定义,确保它们返回的是有效的引用或指针。 -
存储中间结果:你可以将表达式的一部分结果存储在一个临时变量中,然后对这个变量进行操作。例如:
auto pmem_handle = dmem->get_gfx_device_memory().get_m_handle_pmem(); auto handle = pmem_handle->get_m_handle(); ppData = &handle.fd_va; // 注意:这里仍然可能有问题,因为handle可能还是临时的 // 如果handle是临时的,你需要确保它包含的数据在ppData使用期间是有效的
但是,这种方法可能不会工作,因为handle
可能仍然是一个临时对象。你需要确保handle
的生命周期足够长,以便在ppData
使用期间它仍然是有效的。
-
直接访问:如果
fd_va
是一个你经常需要访问的变量,考虑将其设计为可以通过类的一个成员函数直接访问,而不是通过一系列的方法调用链。 -
使用智能指针:如果可能的话,使用智能指针(如
std::shared_ptr
或std::unique_ptr
)来管理资源,这样可以确保对象的生命周期得到正确的管理。
总之,这个问题通常涉及到对对象生命周期和临时对象的理解。你需要检查并调整你的代码,以确保在需要的时候能够安全地访问所需的数据。
编译错误4: missing default argument on parameter
gralloc/wsialloc/android/src/helper_functions.h:35:15: error: use of undeclared identifier 'PAGE_SIZE'
35 | return (x + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
编译错误原因:
Using a compile-time constant for the page size of your system is typically not a smart thing to do, as it's not necessarily constant. For example, on the latest x86 CPUs, your operating system can choose between using 4 KiB, 2 MiB and even 1 GiB pages (or even combine them in the same address space). For this reason, POSIX hasn't standardized the PAGE_SIZE
constant.
Many systems therefore provide a getpagesize()
function, but keep in mind that the POSIX standardized way of obtaining the page size is by using the sysconf()
function:
解决方案:
#include <stdio.h> #include <unistd.h> int main() { printf("%lu\n", sysconf(_SC_PAGESIZE)); }
即把PAGE_SIZE替换为 sysconf(_SC_PAGESIZE)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话