admin管理员组文章数量:1590153
目录
1.[Error] statement cannot resolve address of overloaded function
2.[Warning] reference to local variable 'a' returned
3.[Error] passing 'const xxx' as 'this' argument of 'void xxx::xxx()' discards qualifiers
4.[Error] jump to case label
1.[Error] statement cannot resolve address of overloaded function
这是调用成员函数时忘记加括号
2.[Warning] reference to local variable 'a' returned
A & A::operator =(const A &b)
{
poly a(b);
this->~A();
return a;
}
这个编译警告是因为在函数中返回了一个指向局部变量的引用。在这个代码段中,函数poly a(b)返回了一个指向局部变量a的引用。为了解决这个问题,你可以使用动态内存分配来创建一个新的poly对象,然后将其返回。这可以通过使用new和delete关键字来实现。例如,你可以将代码修改为:
A* a = new A(b);
return *a;
3.[Error] passing 'const xxx' as 'this' argument of 'void xxx::xxx()' discards qualifiers
class A
{
void out();
void somefunction(const A &b);
};
void A::out()
{
return ;
}
void somefunction(const A &b)
{
b.out();
return b;
}
这个错误是因为你在调用 b.out() 时,b是一个 const 对象,但是 out() 函数并没有被声明为 const 函数,因此编译器会认为你在修改 b,从而报错。要解决这个问题,你需要在 A 类中将 out() 函数声明为 const 函数,即在函数声明和定义中都加上 const 修饰符:
void out() const;
void A::out() const
{
}
当一个成员函数被声明为 const 函数时,它表明该函数不会修改对象的状态。
4.[Error] jump to case label
出现这个问题的原因是,在switch-case语句中定义了局部变量;
switch(xxx)
{
case 1:
int n;
case 2:
case 3:
}
解决办法:
1.用{ }把case语句括起来;(添加了作用域)
switch(xxx)
{
case 1:
{ int n; }
case 2:
case 3:
}
2.把变量的定义提到switch前;
版权声明:本文标题:经典编译错误&警告收集 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1728084304a1144804.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论