gpt4 book ai didi

无法识别 C++ 虚函数

转载 作者:太空宇宙 更新时间:2023-11-04 15:49:28 27 4
gpt4 key购买 nike

我正在使用 CodeBlocks,但我有以下无法编译的代码。

(这是关于一些 C++ 陷阱所以我唯一想问的是为什么它不编译)

代码如下:

#include <iostream>
using namespace std;

class Shape
{
public:
Shape();
virtual void reset();
private:
int color;
};

class Point : public Shape
{
private:
double a,b;
};

void Shape::reset()
{
cout<<"Shape reset\n";
}

void Point::reset()
{
Shape::reset();
cout<<"Point reset";
}

Shape::Shape()
{
reset();
}

int main()
{
Shape s;
Point o;
}

我收到以下错误:

no `void Point::reset()' member function declared in class `Point'

最佳答案

您需要将函数声明添加到您的 Point 类主体中:

class Point : public Shape
{
public:
virtual void reset();
private:
double a,b;
};

(virtual 是不必要的,因为它在基类中被声明为virtual。但是添加它作为提醒是有帮助的。)

关于无法识别 C++ 虚函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10872660/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com