Q1) 下記のコードがコンパイル出来る様にする方法はありますか?
#include <stdio.h>
// 抽象クラス
struct Animal {
virtual const char* makeSound() = 0; // 純粋仮想関数
void say() { puts(makeSound()); }
};
// サブクラスで仮想関数をオーバーライド
struct Cat : Animal {
const char* makeSound() { return "にゃー!"; }
};
struct Dog : Animal {
const char* makeSound() { return "ワン!"; }
};
int main(int argc, const char* argv[]) {
Cat cat; <ーー不明です
Dog dog;
Cat().say(); <ーー不明です
Dog().say();
// 親クラスは抽象クラスになるため、インスタンス化が出来なくなる
//Animal animal; // Error: Variable type 'Animal' is an abstract class
//Animal(); // Error: Allocating an object of abstract class type 'Animal'
}
gcc main.cpp コンパイルエラーの表示
/tmp/ccfTgxPV.o:(.rodata._ZTV6Animal[_ZTV6Animal]+0x8): `__cxa_pure_virtual' に対する定義されていない参照です
お手数ですが宜しくお願いします。

回答2件
あなたの回答
tips
プレビュー