经典编译错误&警告收集

目录

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前; 

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值