ich folgende Testprogramm habenWie löst man das Problem mit Diamanten in C++?
#include<iostream>
using namespace std;
class Faculty {
// data members of Faculty
public:
Faculty(int x) {
cout<<"Faculty::Faculty(int) called"<< endl;
}
void test() {
cout<<"Faculty::test called" << endl;
}
};
class Student {
// data members of Student
public:
Student(int x) {
cout<<"Student::Student(int) called"<< endl;
}
void test() {
cout<<"Student::test called" << endl;
}
};
class TA : virtual public Faculty, virtual public Student {
public:
TA(int x):Student(x), Faculty(x) {
cout<<"TA::TA(int) called"<< endl;
}
};
int main() {
TA ta1(30);
ta1.test();
}
aN Fehler während der Kompilierung bekommen
8be257447d8c26ef785b1a60f2884a.cpp: In function 'int main()':
748be257447d8c26ef785b1a60f2884a.cpp:36:6: error: request for member 'test' is ambiguous
ta1.test();
^
748be257447d8c26ef785b1a60f2884a.cpp:22:7: note: candidates are: void Student::test()
void test() {
^
748be257447d8c26ef785b1a60f2884a.cpp:11:7: note: void Faculty::test()
void test() {
^
Auch virtuelle Vererbung hier verwende ich. Irgendeine Lösung dafür?
Hier ist kein Diamant. Sie haben nur einen Namenskonflikt von zwei nicht verwandten Basisklassen, und virtuelle Vererbung kann nichts dagegen tun. Werfen Sie Lösungen, die Sie nicht verstehen, nicht auf andere Probleme. –
irgendeine Lösung auf diesem? – BSalunke
Versuchen Sie 'ta1.Student :: test()' oder 'ta1.Faculty :: test()' – GAVD