error: request for member is ambiguous c++;
error: request for member is ambiguous c++;
我已经在 Whosebug 上查看了几个类似的问题,但没有任何帮助。我不明白为什么我的 class NetflixCanada 中的关键字 using 不能解决歧义。
我想达到的目标:
第二次访问来自 NetflixUkraine 的私有方法 class.
无论如何,这是代码:
#include <iostream>
using namespace std;
void greetings(){
cout << "\e[1mHi! This is a Netflix country review:\e[0m\n";
}
class NetflixUkraine {
private:
string mostPplrBrowserFrNTflx;
public:
string countryName;
int amountMedia = 5000;
void uniqueMedia(){
cout << "We have john Wick II\n";
}
void setBrowser(string amostPplrBrowserFrNTflx){
if (amostPplrBrowserFrNTflx == "Chrome" || amostPplrBrowserFrNTflx == "Firefox" ||
amostPplrBrowserFrNTflx == "Safari" || amostPplrBrowserFrNTflx == "Opera"){
mostPplrBrowserFrNTflx = amostPplrBrowserFrNTflx;
}
else {
mostPplrBrowserFrNTflx = "\nCannot define a web browser!\n";
}
}
string getBrowser(){
return mostPplrBrowserFrNTflx;
}
};
class NetflixPoland: public NetflixUkraine {
public:
void uniqueMedia(){
cout << "We have john Wick II and III\n"; //method overriding
}
string doPolesLoveNtflx(bool answer){ //extending inheritance
if (answer == 1){
return "Poles love Netflix";
}
else{
return "Poles don't love Netflix";
}
}
};
class NetflixCanada: public NetflixUkraine, public NetflixPoland{
public:
using NetflixUkraine::setBrowser;
};
int main()
{
greetings();
//printing output
NetflixUkraine library1;
library1.countryName = "Ukraine";
cout<< library1.countryName<<endl << library1.amountMedia << endl;
library1.uniqueMedia(); //printing out the unique media
NetflixPoland library3;
library3.countryName = "\nPoland";
cout << library3.countryName<< endl;
library3.uniqueMedia();
cout << library3.doPolesLoveNtflx(true) << endl;
//adding a fav browser for watching Netflix
library3.setBrowser("Chrome");
cout << "most popular browser for watching Netflix: " << library3.getBrowser() << endl;
NetflixCanada library4;
library4.NetflixUkraine::setBrowser ("Chrome");
return 0;
}
输出:
[12inheritance.cpp 2020-10-10 11:41:07.556]
,,12inheritance.cpp:65:7: warning: direct base ‘NetflixUkraine’ inaccessible in ‘NetflixCanada’ due to ambiguity
65 | class NetflixCanada: public NetflixUkraine, public NetflixPoland{
| ^~~~~~~~~~~~~
12inheritance.cpp: In function ‘int main()’:
12inheritance.cpp:96:30: error: ‘NetflixUkraine’ is an ambiguous base of ‘NetflixCanada’
96 | library4.NetflixUkraine::setBrowser ("Chrome");
| ^~~~~~~~~~
你的编译器无法区分:
Netflix加拿大::Netflix乌克兰::setBrowser
和
Netflix加拿大::Netflix波兰::Netflix乌克兰::setBrowser
如评论中所述,请阅读 "The deadly diamond of death"
我已经在 Whosebug 上查看了几个类似的问题,但没有任何帮助。我不明白为什么我的 class NetflixCanada 中的关键字 using 不能解决歧义。
我想达到的目标: 第二次访问来自 NetflixUkraine 的私有方法 class.
无论如何,这是代码:
#include <iostream>
using namespace std;
void greetings(){
cout << "\e[1mHi! This is a Netflix country review:\e[0m\n";
}
class NetflixUkraine {
private:
string mostPplrBrowserFrNTflx;
public:
string countryName;
int amountMedia = 5000;
void uniqueMedia(){
cout << "We have john Wick II\n";
}
void setBrowser(string amostPplrBrowserFrNTflx){
if (amostPplrBrowserFrNTflx == "Chrome" || amostPplrBrowserFrNTflx == "Firefox" ||
amostPplrBrowserFrNTflx == "Safari" || amostPplrBrowserFrNTflx == "Opera"){
mostPplrBrowserFrNTflx = amostPplrBrowserFrNTflx;
}
else {
mostPplrBrowserFrNTflx = "\nCannot define a web browser!\n";
}
}
string getBrowser(){
return mostPplrBrowserFrNTflx;
}
};
class NetflixPoland: public NetflixUkraine {
public:
void uniqueMedia(){
cout << "We have john Wick II and III\n"; //method overriding
}
string doPolesLoveNtflx(bool answer){ //extending inheritance
if (answer == 1){
return "Poles love Netflix";
}
else{
return "Poles don't love Netflix";
}
}
};
class NetflixCanada: public NetflixUkraine, public NetflixPoland{
public:
using NetflixUkraine::setBrowser;
};
int main()
{
greetings();
//printing output
NetflixUkraine library1;
library1.countryName = "Ukraine";
cout<< library1.countryName<<endl << library1.amountMedia << endl;
library1.uniqueMedia(); //printing out the unique media
NetflixPoland library3;
library3.countryName = "\nPoland";
cout << library3.countryName<< endl;
library3.uniqueMedia();
cout << library3.doPolesLoveNtflx(true) << endl;
//adding a fav browser for watching Netflix
library3.setBrowser("Chrome");
cout << "most popular browser for watching Netflix: " << library3.getBrowser() << endl;
NetflixCanada library4;
library4.NetflixUkraine::setBrowser ("Chrome");
return 0;
}
输出:
[12inheritance.cpp 2020-10-10 11:41:07.556]
,,12inheritance.cpp:65:7: warning: direct base ‘NetflixUkraine’ inaccessible in ‘NetflixCanada’ due to ambiguity
65 | class NetflixCanada: public NetflixUkraine, public NetflixPoland{
| ^~~~~~~~~~~~~
12inheritance.cpp: In function ‘int main()’:
12inheritance.cpp:96:30: error: ‘NetflixUkraine’ is an ambiguous base of ‘NetflixCanada’
96 | library4.NetflixUkraine::setBrowser ("Chrome");
| ^~~~~~~~~~
你的编译器无法区分:
Netflix加拿大::Netflix乌克兰::setBrowser
和
Netflix加拿大::Netflix波兰::Netflix乌克兰::setBrowser
如评论中所述,请阅读 "The deadly diamond of death"