无法转换为无法访问的 Person 基类,我该怎么办?
链接到与本主题相关的最后一个问题:我不太明白组类应该如何编写?
new Soldier 首先在组类中创建 这是类层次结构:
第一.h:
#pragma once
#include "Division.h"
class first : public Division
{
public:
first(string Name);
~first();
void build();
void Show();
};
第一个.cpp:
#include "first.h"
#include "Soldier.h"
first::first(string Name) : Division(name)
{
name = Name;
}
first::~first()
{
}
void first::build()
{
_persons.push_back(new Soldier);
_persons.push_back(new Soldier);
}
void first::Show()
{
cout << "Division: " << name << endl;
for (auto person : _persons)
person->Show();
}
人.h:
#pragma once
#include "Object.h"
#include <iostream>
#include <cstring>
using namespace std;
class Person : public Object
{
protected:
string name;
public:
Person();
Person(string name);
~Person();
virtual void show();
};
士兵.h:
#pragma once
#include "Person.h"
class Soldier : protected Person
{
public:
Soldier();
Soldier(string Name);
~Soldier();
};
师.h:
#pragma once
#include "Object.h"
#include "Person.h"
#include <list>
#include "Soldier.h"
class Division : public Object
{
protected:
string name;
list<Person*> _persons;
public:
Division(string Name);
Division(Division&);
~Division();
void addPerson(Person* p);
virtual void Show() = 0;
virtual void build() = 0;
};


您有上一篇文章中出现的错误
你
private/protected/public有点太聪明了——你的公共方法在父母身上徘徊在受保护的状态,而在孩子身上又是公共的:)如果完成
那么一切都会奏效
附言
并决定方法 - 你有
show或Show