Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;继承、多态性、运算符重载_C++_Inheritance_Operator Overloading - Fatal编程技术网

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

C++ C++;继承、多态性、运算符重载,c++,inheritance,operator-overloading,C++,Inheritance,Operator Overloading,在编译以下代码时,我遇到了很多问题(其中一些问题已经解决)。我刚刚开始C++,现在学习操作符重写和多态继承等等。我想这个Peon是一个受害者。巫师可以使它们变形 以下是我确实存在的许多错误中的一些错误: Peon.hh:11:1: error: expected class-name before ‘{’ token { ^ In file included from Victim.hh:4:0, from Victim.cpp:11: Sorcerer.h

在编译以下代码时,我遇到了很多问题(其中一些问题已经解决)。我刚刚开始C++,现在学习操作符重写和多态继承等等。我想这个Peon是一个受害者。巫师可以使它们变形

以下是我确实存在的许多错误中的一些错误:

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;
              ^~~~~~
我在挣扎,你能帮忙吗

Main.cpp
#包括“巫师.hh”
#包括“受害者.hh”
#包括“paun.hh”
内部主(空)
{
魔术师罗伯特(“罗伯特”,“大帝”);
受害者吉姆(“吉米”);
牡丹乔(“乔”);

std::cout首先去掉#之间的空格,并在所有头保护中定义,然后尝试编译

第二,相应地分配传递给巫师和受害者构造函数的参数

#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);
}
    #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_ */
#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;
}
#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_ */
#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_ */