C++ 继承、多态、运算符重载

C++ Inheritance, polymorphism, operator overloading

本文关键字:重载 运算符 多态 C++ 继承      更新时间:2023-10-16

我在编译以下代码时遇到了很多问题(其中一些已经修复)。我刚刚开始C++,目前正在学习运算符覆盖和多态继承等。我希望牡丹成为IS-A的受害者。巫师可以多晶型他们。

以下是我遇到的许多错误中的一些错误:

Peon.hh:11:1: error: expected class-name before ‘{’ token
 {
 ^
In file included from Victim.hh:4:0,
                 from Victim.cpp:11:
Sorcerer.hh:20:18: error: ‘Victim’ has not been declared
   void polymorph(Victim const &) const;
              ^~~~~~

我在挣扎,你能帮忙吗?

主.cpp

#include "Sorcerer.hh"
#include "Victim.hh"
#include "Peon.hh"
int     main(void)
{
  Sorcerer robert("Robert", "the Magnificient");
  Victim jim("Jimmy");
  Peon joe("Joe");
  std::cout << robert << jim < joe;
  robert.polymorph(jim);
  robert.polymorph(joe);
  return (0);
}

巫师.cpp

    #include "Sorcerer.hh"
Sorcerer::Sorcerer(std::string name, std::string title)
{
  std::cout << name << ", " << title << " is born !" << std::endl;
}
Sorcerer::~Sorcerer()
{
  std::cout << this->_name << ", " << this->_title << " is dead. Consequences will never be the same !" << std::endl;
}
std::ostream& operator<<(std::ostream& out, const Sorcerer &sorcerer)
{
  return out << "I am " << sorcerer.getName() << ", " << sorcerer.getTitle() << " and I like ponies !";
}
std::string Sorcerer::getName() const
{
  return this->_name;
}
std::string Sorcerer::getTitle() const
{
  return this->_title;
}
void Sorcerer::polymorph(Victim const &victim) const
{
  victim.getPolymorphed();
}

巫师。

#ifndef SORCERER_HH_
# define SORCERER_HH_
#include "Victim.hh"
#include "Peon.hh"
#include <iostream>
#include <ostream>
#include <string>
#include <iomanip>
class           Sorcerer {
  std::string   _name;
  std::string   _title;
public:
  Sorcerer(std::string, std::string);
  ~Sorcerer();
  std::string   getName() const;
  std::string   getTitle() const;
  void polymorph(Victim const &) const;
  virtual void getPolymorphed() const;
};
#endif /* !SORCERER_HH_ */

受害者.cpp

#include "Victim.hh"
Victim::Victim(std::string name)
{
  std::cout << "Some random victim called " << name << " just popped !" << std::endl;
}
Victim::~Victim()
{
  std::cout << "Victim " << this->_name << " just died for no apparent reason !" << std::endl;
}
std::ostream& operator<<(std::ostream& out, const Victim &victim)
{
  return out << "I'm " << victim.getName() << " and i like otters !";
}
std::string Victim::getName() const
{
  return this->_name;
}
void Victim::getPolymorphed() const
{
  std::cout << this->_name << " has been turned into a cute little sheep !" << std::endl;
}

受害者.hh

#ifndef VICTIM_HH_
# define VICTIM_HH_
#include "Sorcerer.hh"
#include "Peon.hh"
#include <iostream>
#include <iomanip>
#include <string>
#include <ostream>
class           Victim {
  std::string   _name;
public:
  Victim(std::string);
  virtual ~Victim();
  virtual void getPolymorphed() const;
  std::string   getName() const;
};
#endif /* VICTIM_HH_ */

牡丹.cpp

#include "Peon.hh"
Peon::Peon(std::string name) : Victim(name)
{
  std::cout << "Zog zog." << std::endl;
}
Peon::~Peon()
{
  std::cout << "Bleuark..." << std::endl;
}
std::string Peon::Peon getName() const
{
  return this->_name;
}
std::ostream& operator<<(std::ostream &out, const Peon &peon)
{
  return out << "I'm " << peon.getName() << " and i like otters !";
}
void Victim::getPolymorphed() const
{
  std::cout << this->_name << " has been turned into a pink pony !" << std::endl;
}

牡丹

#ifndef PEON_HH_
# define PEON_HH_
#include "Sorcerer.hh"
#include "Victim.hh"
#include <iostream>
#include <iomanip>
#include <string>
class           Peon : public Victim
{
  std::string   _name;
public:
  Peon(std::string);
  virtual ~Peon();
  virtual void getPolymorphed() const;
  std::string   getName() const;
};
#endif /* !PEON_HH_ */
首先去

掉 # 和定义之间的空格,然后尝试编译。

其次,相应地分配你在巫师和受害者构造函数中传递的参数。