2

C++的各种报错和报警

 1 year ago
source link: https://charon-cheung.github.io/2023/02/01/C++/C++%20%20%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/C++%E7%9A%84%E5%90%84%E7%A7%8D%E6%8A%A5%E9%94%99%E5%92%8C%E6%8A%A5%E8%AD%A6/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

C++的各种报错和报警 | 沉默杀手

C++的各种报错和报警
2023-02-01|C++C++ 面向对象|
Word count: 263|Reading time: 1 min

还是Qt的环境好,很多时候,不用编译直接就提示有错误。

  • 报错invalid new-expression of abstract class type

有的编译器也会报错 error: allocating an object of abstract class type ‘child’。说明基类中有纯虚函数没有实现,必须在派生类实现所有纯虚函数,不使用派生类时是看不出来的。

  • marked ‘override’, but does not override
class Parent
{
public:
virtual std::string getName1(int x)
{
return "Parent";
}

virtual std::string getName2(int x)
{
return "Parent";
}
};

class Child : public Parent
{
public:
virtual std::string getName1(double x) override
{
return "Sub";
}
virtual std::string getName2(int x) const override
{
return "Sub";
}
};

派生类的同名函数和基类的参数类型不同,而且多了const。如果没有override,那么是派生类新加的函数,但是添加override,编译器认为基类有同名函数,但是没发现,所以报错。

如果是重写基类方法,建议使用override标识符,让编译器检查出可能的错误,从而避免无意的错误。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK