简体   繁体   中英

C++ Inheritance, polymorphism, operator overloading

I have lots of problems (some of them were already fixed) when compiling the following code. I'm just starting in C++ and currently learning operator overloding and polymorphism inheritance, etc. I'd like the Peon to be IS-A Victim. The Sorcerer can polymorph them.

Here are some errors out of many I do have:

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;
              ^~~~~~

I'm struggling, could you please help?

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);
}

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

Sorcerer.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_ */

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

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

Peon.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_ */

First get rid of the space between the # and define in all your header guards, then try and compile.

Second, assign the arguments you're passing in Sorcerer and Victim constructor accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM