Enviado em 14/05/2024 - 12:07h
O meu código:#include <iostream>
class FlyBehavior{
public:
virtual void fly() = 0;
};
class FlyWithWings: public FlyBehavior{
public:
void fly(){
std::cout << "I'm flying!!!\n";
};
};
class FlyNoWay: public FlyBehavior{
public:
void fly(){
std::cout << "I can't fly\n";
};
};
class QuackBehavior{
public:
virtual void quack() = 0;
};
class Quack: public QuackBehavior{
public:
void quack();
};
class Squeak: public QuackBehavior{
public:
void quack(){
std::cout << "Quack\n";
};
};
class MuteQuack: public QuackBehavior{
public:
void quack(){
std::cout << "Silence\n";
};
};
class Duck{
public:
FlyBehavior* flyBehavior;
QuackBehavior* quackBehavior;
void performQuack(){
quackBehavior->quack();
};
void swim();
virtual void display() = 0;
void performFly();
};
class MallardDuck: public Duck{
public:
MallardDuck(){
quackBehavior = new Quack();
flyBehavior = new FlyWithWings();
};
void display(){
std::cout << "I'm a real Mallard Duck\n";
};
};
int main(){
Duck* mallard = new MallardDuck;
mallard->performQuack();
mallard->performFly();
}
$ g++ main.cc
/usr/bin/ld: /tmp/ccq15Ryy.o: aviso: relocalização contra "_ZTV5Quack" em secção só de leitura ".text._ZN5QuackC2Ev[_ZN5QuackC5Ev]"
/usr/bin/ld: /tmp/ccq15Ryy.o: na função "main":
main.cc:(.text+0x40): undefined reference to `Duck::performFly()'
/usr/bin/ld: /tmp/ccq15Ryy.o: na função "Quack::Quack()":
main.cc:(.text._ZN5QuackC2Ev[_ZN5QuackC5Ev]+0x1b): undefined reference to `vtable for Quack'
/usr/bin/ld: aviso: a criar DT_TEXTREL num PIE.
collect2: error: ld returned 1 exit status