C++ 继承、多态、运算符重载
C++ Inheritance, polymorphism, operator overloading
我在编译以下代码时遇到了很多问题(其中一些已经修复)。我刚刚开始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_ */
首先去
掉 # 和定义之间的空格,然后尝试编译。
其次,相应地分配你在巫师和受害者构造函数中传递的参数。
相关文章:
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 重载运算符new[]的行为取决于析构函数
- 为什么将值返回函数传递给重载=运算符对运算符函数有效,而对其他运算符无效
- 在 myVector 类中重载运算符 + 时出错
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 如何在 cpp 中重载运算符 +=?
- C++ 如何重载 [] 运算符并进行函数调用
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- 重载运算符有地址吗?
- 如何迭代重载运算符 [] 的类?
- 重载运算符与添加问题
- 模板基类中的重载运算符
- 如何调用用于重载运算符"<<"的 friend 函数?
- 在 C++17 中的命名空间和子命名空间中重载运算符是不明确的
- 重载运算符<<采用谷歌 C++ 风格
- C++ 如何正确重载 + 运算符
- cout (<<) 重载运算符不打印减去的矩阵
- 如何在 c++ 中重载运算符 + 以便能够 whrite c_str = "smth" + c_str;
- 重载运算符*以获取对另一个类的实例的引用