以下问题: 我得到了一个抽象的motherclass,两个子类和一个使用子类的函数的类。必须实现继承的母类虚拟方法
motherclass:
Vektor.h
class Vektor {
public:
Vektor();
virtual bool range() const = 0;
virtual ~Vektor();
};
子类:
Absolutvektor.h
class Absolutvektor: public Vektor {
public:
Absolutvektor(int x, int y);
int x;
int y;
virtual ~Absolutvektor();
virtual bool range (int x, int y);
};
Einheitsvektor.h
class Einheitsvektor : public Vektor {
public:
Einheitsvektor(int x, int y, int richtung);
int x;
int y;
int richtung;
virtual ~Einheitsvektor();
virtual bool Einheitsvektor::range (int richtung);
};
“正常班”: schiff.h
#pragma once
#include <Einheitsvektor.h>
#include <Absolutvektor.h>
class Schiff {
public:
Schiff(int schiffId, int schiffLaenge, Absolutvektor absolutvektor, Einheitsvektor einheitsvektor, bool segmentestati[]);
int schiffId;
int schiffLaenge;
Absolutvektor absolutvektor;
Einheitsvektor einheitsvektor;
bool segmentestati[] = false;
// c++ vektor
};
Schiff.cpp:
#include "Schiff.h"
Schiff::Schiff(int schiffId, int schiffLaenge, Absolutvektor absolutvektor, Einheitsvektor einheitsvektor, bool segmentestati[]){
this -> schiffId = schiffId;
this -> schiffLaenge = schiffLaenge;
}
洙现在我得到以下错误在希夫文件:
类型'Einheitsvektor'必须实现继承的纯虚拟 我的ThOD“VEKTOR ::范围”
错误出现六次,在Schiff.cpp 2倍和4倍Schiff.h
我不知道为什么要实现该功能为“VEKTOR”因为这是一个抽象类,并且我有两个不同的函数范围用例。
虚拟函数上的签名需要与实现中的签名相匹配。 – Tyler
'bool range();''和'bool range(int richtung);'不是同一个函数。您可能认为您在'Einheitsvektor :: range'中重写'Vektor :: range',但C++不会。如果你的意思是重写一个函数,使用'override'关键字。 – nwp