【问题标题】:C++ Inheritance, polymorphism, operator overloadingC++ 继承、多态、运算符重载
【发布时间】:2017-01-13 23:58:50
【问题描述】:

在编译以下代码时,我遇到了很多问题(其中一些已经修复)。我刚从 C++ 开始,目前正在学习运算符重载和多态继承等。我希望 Peon 成为 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;
              ^~~~~~

我在苦苦挣扎,你能帮忙吗?

Main.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();
}

巫师.hh

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

Peon.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;
}

苦工.hh

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

【问题讨论】:

    标签: c++ inheritance operator-overloading


    【解决方案1】:

    首先去掉所有标头保护中的 # 和定义之间的空格,然后尝试编译。

    其次,相应地分配您在 Sorcerer 和 Victim 构造函数中传递的参数。

    【讨论】:

      猜你喜欢
      • 2011-12-05
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多