![](/img/trans.png)
[英]Why class member function destroies the memory allocated for a pointer argument?
[英]Why is OnlineGDB ignoring the second argument in my member function?
.h 类中的函数原型包含 2 个参数,.cpp 类中的函数包含相同的 2 个参数,但编译器假装它只有 1 个(忽略第二个)并打印以下错误:
void list::swap(const string[] from, const string[] to)
^~~~
list.cpp:103:6: error: prototype for 'void list::swap(const string*)' does not match any in class 'list'
void list::swap(const string[] from, const string[] to)
^~~~
In file included from list.cpp:6:0:
list.h:83:10: error: candidate is: void list::swap(const string*, const string*)
void swap(const string[], const string[]);
^~~~
我一开始以为可能是内存问题,但我有 26 GB 的 RAM,而 Web 浏览器只使用了 3GB。 我尝试刷新并重新启动计算机。 你可以用这个截断的代码亲眼看看: https : //onlinegdb.com/-L_eUQTzY
括号必须在名称之后:
void list::swap(const string from[], const string to[])
因此,它假设第一个const string[]
是传递的参数并忽略该行上的其他所有内容,因为它需要在参数名称之后有一个 ',' 或 '...' ,然后再写入其他内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.