我有不同的树类,我们将其称为Locomotion,Walk和TimeInfo。
class TimeInfo {
int timeAtEnd() {return mTimeAtEnd;}
}
class Locomotion {
// Stuff
virtual float getDistanceTravelled() const = 0;
virtual int getTimeAtEnd() const = 0;
}
class Walk : public Locomotion, public TimeInfo {
// Stuff
float Walk::getDistanceTravelled() const {return mDistanceTravelled;} // Works
int Walk::getTimeAtEnd() const {return TimeInfo::timeAtEnd();} // DO NOT WORK
}
我的问题是,在程序的另一部分,我可以访问指向Locomotion对象的指针,该指针实际上可以指向Walk对象,或者指向同时扩展Locomotion和TimeInfo的某些其他类。从这个Locomotion指针中,我需要能够获得timeAtEnd值。我在执行此操作时遇到问题,因为我不允许Locomotion本身扩展TimeInfo,并且当我尝试使用上述代码进行编译时,出现以下错误:
error: non-member function 'int TimeInfo::timeAtEnd()' cannot have cv-qualifier
据我所知,问题是我实际上没有在Walk :: getTimeAtEnd()]中使用TimeInfo对象,但我不知道如何解决此问题...
我有不同的树类,我们将其称为Locomotion,Walk和TimeInfo。 class TimeInfo {int timeAtEnd(){return mTimeAtEnd;}} class Locomotion {//东西虚拟浮点数...
您需要将timeAtEnd()设置为常量,然后它将起作用。