Устранить неоднозначность, возникающую в результате множественного наследования классов, которые имеют общий шаблонный класс

Я пытаюсь создать базовый класс, который является шаблоном класса и принимает, как шаблон, некоторый класс. Это базовый класс в родительском классе двух других классов, которые сами являются родителями последнего класса. Таким образом, у меня есть традиционная проблема с алмазом в c ++ с добавлением, что корневой базовый класс является классом шаблона. Использование виртуального наследования здесь не совсем подходит.

Изменить: Включен некоторый код (и исправлены опечатки)

#include<iostream>
#include<string>

using namespace std;

template<class TemplateT>
class MainMaster {
protected:
std::string name;
int number;
TemplateT variableProp;

public:
explicit MainMaster(std::string, int);

void setName(std::string);
std::string getName();
void printName();

void setNumber(int);
int getNumber();
void printNumber();

void printMasterProperties(std::string);

void setVarProp(TemplateT);
TemplateT getVarProp();
};

template<class TemplateT>
MainMaster<TemplateT>::MainMaster(std::string nameIn, int numIn){
setName(nameIn);
setNumber(numIn);
}

template<class TemplateT>
void MainMaster<TemplateT>::setName(std::string nameIn){ name = nameIn; }

template<class TemplateT>
std::string MainMaster<TemplateT>::getName(){ return name; }

template<class TemplateT>
void MainMaster<TemplateT>::printName(){ cout << "Master's name is " << name << endl; }

template<class TemplateT>
void MainMaster<TemplateT>::setNumber(int numIn){ number = numIn; }

template<class TemplateT>
int MainMaster<TemplateT>::getNumber(){ return number; }

template<class TemplateT>
void MainMaster<TemplateT>::printNumber(){ cout << name << "'s number is " << number << endl; }

template<class TemplateT>
void MainMaster<TemplateT>::printMasterProperties(std::string pre = ""){
cout << pre << "Master Properties" << endl
<< pre << "-Number: " << number << endl;
}

class ChildOne: public virtual MainMaster<std::string>{
protected:
std::string propOne;

public:
//      using MainMaster::MainMaster;
ChildOne(std::string nameIn, int numIn, std::string p1);
void printName();
void setPropOne(std::string);
std::string getPropOne();

void printOnesProps(std::string);
};

ChildOne::ChildOne(std::string nameIn, int numIn, std::string p1): MainMaster<std::string>(nameIn,numIn){
setPropOne(p1);
}

void ChildOne::printName(){ cout << "ChildOne's name is " << name << endl; }

void ChildOne::setPropOne(std::string propIn){ propOne = propIn; }

std::string ChildOne::getPropOne(){ return propOne; }

void ChildOne::printOnesProps(std::string pre = ""){
printMasterProperties("-");
cout << pre << "One Properties" << endl
<< pre << "-PropOne: " << propOne << endl;
}

class ChildTwo: public virtual MainMaster<int>{
protected:
std::string propTwo;

public:
ChildTwo(std::string nameIn, int numIn, std::string p2);
void printName();

void setPropTwo(std::string);
std::string getPropTwo();

void printTwosProps(std::string);
};

ChildTwo::ChildTwo(std::string nameIn, int numIn, std::string p2): MainMaster<int>(nameIn,numIn){
setPropTwo(p2);
}

void ChildTwo::printName(){
cout << "ChidTwo's name is " << name << endl;
}

void ChildTwo::setPropTwo(std::string propIn){ propTwo = propIn; }

std::string ChildTwo::getPropTwo(){ return propTwo; }

void ChildTwo::printTwosProps(std::string pre = ""){
printMasterProperties("-");
cout << pre << "Two Properties" << endl
<< pre << "-PropTwo: " << propTwo << endl;
}

class FinalChild: public ChildOne, public ChildTwo{
protected:
double finalProp;

public:
FinalChild(std::string nameIn, int num, std::string prop1 ,std::string prop2, double pFinal);
void printFinalProps(std::string);
};

FinalChild::FinalChild(std::string nameIn, int num, std::string prop1 ,std::string prop2, double pFinal):
ChildOne(nameIn, num, prop1),
ChildTwo(nameIn, num, prop2),
MainMaster(nameIn, num){
finalProp = pFinal;
}

void FinalChild::printFinalProps(std::string pre = ""){
printMasterProperties("-");
cout << pre << name << "'s Final Properties" << endl;
cout << pre << "-Number: " << number << endl;
cout << pre << "-PropOne: " << propOne << endl;
cout << pre << "-PropTwo: " << propTwo << endl;
cout << pre << "-finalProp " << finalProp << endl;
}

int main () {
MainMaster<char> master("Master", 0);
ChildOne child1("Child1",1,"P1One");
ChildTwo child2("Child2",2,"P2Two");
FinalChild finalC("FinalChild", 3, "P1Final", "P2Final", 3.0);

master.printMasterProperties();
child1.printOnesProps();
child2.printTwosProps();
finalC.printFinalProps();}

1

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector