问题描述
我在这里有很多代码,但是恐怕这个代码可以用来表达问题,所以请与我一起:
I have a lot of code here but I'm afraid this is as little code as I could put to convey the problem, so please bear with me:
#include <iostream>
#define ASINSTANCE(x, type, y) \
type * y = dynamic_cast<type *>(&(x)); \
if (y)
class Fruit {
virtual void a() = 0; // This is to surpress the "Fruit isn't polymorphic" we'd otherwise get.
};
class Apple : public Fruit {
virtual void a() {
}
};
class Orange : public Fruit {
virtual void a() {
}
};
class Banana : public Fruit {
virtual void a() {
}
};
template<typename FruitType>
class FruitEater {
protected:
virtual void eat(const FruitType & t) = 0;
};
template<typename... FruitTypes>
class MultiFruitEater : public FruitEater<FruitTypes>... {
public:
// Eat any fruit if it belongs to FruitTypes (returns false otherwise).
bool dispatchEat(const Fruit & fruit);
private:
template<typename First>
bool dispatchEatByType(const Fruit & fruit);
template<typename First, typename Second, typename... Rest>
bool dispatchEatByType(const Fruit & fruit);
};
class MyEater : public MultiFruitEater<Apple, Orange, Banana> {
protected:
virtual void eat(const Apple & t);
virtual void eat(const Orange & t);
virtual void eat(const Banana & t);
};
void MyEater::eat(const Apple & t) {
std::cout << "Ate apple." << std::endl;
}
void MyEater::eat(const Orange & t) {
std::cout << "Ate orange." << std::endl;
}
void MyEater::eat(const Banana & t) {
std::cout << "Ate banana." << std::endl;
}
template<typename... FruitTypes>
bool MultiFruitEater<FruitTypes...>::dispatchEat(const Fruit & fruit) {
return dispatchEatByType<FruitTypes...>(fruit);
}
template<typename... FruitTypes>
template<typename First>
bool MultiFruitEater<FruitTypes...>::dispatchEatByType(const Fruit & fruit) {
ASINSTANCE(fruit, const First, pCastFruit) {
eat(*pCastFruit);
return true;
}
return false;
}
template<typename... FruitTypes>
template<typename First, typename Second, typename... Rest>
bool MultiFruitEater<FruitTypes...>::dispatchEatByType(const Fruit & fruit) {
ASINSTANCE(fruit, const First, pCastFruit) {
eat(*pCastFruit);
return true;
}
return dispatchEatByType<Second, Rest...>(fruit);
}
int main() {
MyEater eater;
Banana b;
eater.dispatchEat(b);
}
问题出在这一行:
eat(*pCastFruit);
我遇到以下错误:
- 错误C2385:吃的模糊访问
- 错误C3861:eat:找不到标识符
我尝试用以下替换行:
this->FruitEater<First>::eat(*pCastFruit);
错误现在更改为:
-
错误LNK2019:未解析的外部符号protected:virtual
void __thiscall FruitEater :: eat(class Apple const&)
(?eat @?$ FruitEater @在函数
中引用的函数VAipple @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ VOorme @VBanana @@@?$ MultiFruitEater @ VApple @@ VOrange @@ VBanana @@@@ AAE_NABVFruit @@@ Z)
error LNK2019: unresolved external symbol "protected: virtualvoid __thiscall FruitEater::eat(class Apple const &)"(?eat@?$FruitEater@VApple@@@@MAEXABVApple@@@Z) referenced in function"private: bool __thiscall MultiFruitEater::dispatchEatByType(class Fruit const &)"(??$dispatchEatByType@VApple@@VOrange@@VBanana@@@?$MultiFruitEater@VApple@@VOrange@@VBanana@@@@AAE_NABVFruit@@@Z)
错误LNK2019:未解决的外部符号protected:virtual
void __thiscall FruitEater :: eat(class Banana const&)
(?eat @?$ FruitEater @ VBanana @@@ MAEXABVBanana @@@ Z) b functionprivate:bool __thiscall MultiFruitEater :: dispatchEatByType(class Fruit
const&)
(?? $ dispatchEatByType @ VBanana @@@?MultiFruitEater @ VApple @@ VOrange @@ VBanana @@ @@ AAE_NABVFruit @@@ Z)
error LNK2019: unresolved external symbol "protected: virtualvoid __thiscall FruitEater::eat(class Banana const &)"(?eat@?$FruitEater@VBanana@@@@MAEXABVBanana@@@Z) referenced infunction "private: bool __thiscall MultiFruitEater::dispatchEatByType(class Fruitconst &)"(??$dispatchEatByType@VBanana@@@?$MultiFruitEater@VApple@@VOrange@@VBanana@@@@AAE_NABVFruit@@@Z)
任何想法?
推荐答案
分辨率:
所以我得到你的回应:
Resolution:
So I got your reponse:
使用你的定义ASSISTANCE:(因为你现在发送的类型不是const
Using Your define ASSISTANCE : (as you gess the type you now send isn't const
#define ASINSTANCE(x, type, y, eater) \
const type * y = dynamic_cast<const type *>(&(x)); \
FruitEater<type>* eater = dynamic_cast<FruitEater< type >*>(this); \
if (y && eater)
在你的fruitEater类:它让你的multieater接受吃方法
In your fruitEater class: (it let your multieater acccess to the eat methode
template<typename FruitType>
class FruitEater {
protected:
template<typename... Fruit> friend class MultiFruitEater;
virtual void eat(const FruitType & t) = 0;
};
如何使用您的新ASSISTANCE define:
How to use your new ASSISTANCE define:
ASINSTANCE(fruit, First, pCastFruit, eater) {
eater->eat(*pCastFruit);
return true;
}
说明:(不够准确)
我认为这个问题( this-> FruitEater< First> :: eat(* pCastFruit);
),是强制编译器使用FruitEater: :eat()methode,wich是一个虚拟的void ...
Explanation: (not as precise as i would want)
I think the problem by doing this (this->FruitEater<First>::eat(*pCastFruit);
), is you force the compiler to use the FruitEater::eat() methode, wich is a virtual void...
这篇关于C ++从乘法继承的模板类调用虚方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!