我正在嘗試在非託管C++中執行類似於C#的顯式接口實現。只要函數實現是類定義的一部分,我就可以做到,但我無法弄清楚如何使它與源文件中的函數實現一起工作。Visual Studio 2010的C++多繼承解決方案問題導致C2509錯誤
這裏是我的測試代碼:
class Base
{
public:
virtual void Func() = 0;
void CallFunc()
{
Func();
}
};
class Base1 : public Base
{
public:
virtual void Func() = 0;
};
class Base2 : public Base
{
public:
virtual void Func() = 0;
};
// This Works
class Multi : public Base1, public Base2
{
public:
virtual void Base1::Func()
{
puts("Base1::Func()");
}
virtual void Base2::Func()
{
puts("Base2::Func()");
}
};
// This does not
// 1>multipeinheritancetest.cpp(60): error C2509: 'Func' : member function not declared in 'Multi'
// 1> multipeinheritancetest.cpp(51) : see declaration of 'Multi'
// 1>multipeinheritancetest.cpp(65): error C2509: 'Func' : member function not declared in 'Multi'
// 1> multipeinheritancetest.cpp(51) : see declaration of 'Multi'
/*
class Multi : public Base1, public Base2
{
public:
virtual void Base1::Func();
virtual void Base2::Func();
};
void Multi::Base1::Func()
{
puts("Base1::Func()");
}
void Multi::Base2::Func()
{
puts("Base2::Func()");
}
*/
// This is a work-around for when I don't want to declare the full function in the header
/*
class Multi : public Base1, public Base2
{
public:
virtual void Base1::Func() { Base1_Func(); }
virtual void Base2::Func() { Base2_Func(); }
void Base1_Func();
void Base2_Func();
};
void Multi::Base1_Func()
{
puts("Base1_Func()");
}
void Multi::Base2_Func()
{
puts("Base2_Func()");
}
*/
void RunMultiTest()
{
Multi m;
m.Base1::CallFunc();
m.Base2::CallFunc();
}
有沒有辦法做到這一點,我的語法只是關閉或這是一個Visual Studio錯誤或者這只是不可能的C++?
問題是如何在類外定義'Base1 :: Func'。該工作示例在類中使用內聯代碼。 – 2011-02-25 17:37:15