我不知道为什么这段代码会崩溃,它看起来对我来说是正确的,但它只是崩溃了一个SIGSEV
. 我来自 Java,习惯于“有用”的错误消息......
主文件
#include <cstdlib>
#include <stdio.h>
#include "DuckV.h"
#include "PenguinV.h"
bool tryFlyOOP(IBird* birdy)
{
return birdy->canFly();
}
int main(int argc, char** argv)
{
DuckV* duckV;
PenguinV* penguinV;
printf("My OOP duck %s fly\n", tryFlyOOP(duckV) ? "can" : "can't");
printf("OOP Penguins %s fly\n", tryFlyOOP(penguinV) ? "can" : "can't");
return 0;
}
鸟:
#ifndef IBIRD_H
#define IBIRD_H
class IBird
{
public:
IBird () {}
virtual bool canFly() {return true;};
};
#endif /* IBIRD_H */
DuckV/PenguinV 除了名字和返回值都一样
#ifndef DUCKV_H
#define DUCKV_H
#include "IBird.h"
class DuckV : public IBird
{
public:
DuckV(){}
virtual bool canFly() {return true;}
};
#endif /* DUCKV_H */
我试过改变周围的东西,但我就是不明白。任何帮助将不胜感激 :)。