【发布时间】:2020-05-31 02:37:23
【问题描述】:
我有这个代码:
//set the AppSystem's Application Vector
void AppSystem::setAppVector(vector<Application *> &applicationVector){
try{
vector<Application *> &tmp1();
tmp1 = dynamic_cast<vector<Application *>>(applicationVector);
if ((tmp1 == NULL) || (applicationVector == NULL)) throw new MyExceptions();
this->ClearAppVector();
this->AppVector = applicationVector;
}catch(MyExceptions e){
return e.ObjectVectorException();
}
}
我收到以下错误:
AppSystem.cpp:23:69:错误:不能 dynamic_cast 'applicationVector'('class std::vector但目标是vector<Application*> 类型的引用。有什么建议吗?
【问题讨论】:
-
向量不是指针。为什么你期望能够
dynamic_cast呢? -
如何投射矢量?
-
你不能。我认为您误解了演员表的作用。
-
所以我得把vector的所有元素都传过来看看是不是Application*类型?
-
@konstantinosDms
throw new MyExceptions()--catch(MyExceptions e)-- 忠告 -- C++ 不是 Java。异常是通过引用而不是值抛出的,C++ 中的new与Java 中的new不同。其次,在高层次上,你到底想完成什么?您的问题听起来更像是XY problem。但总的来说,您的代码看起来像是使用 Java 作为模型编写 C++ 代码的尝试。那将永远行不通——代码要么有问题、有内存泄漏、效率低下,要么对 C++ 程序员来说看起来很奇怪。
标签: c++ vector dynamic-cast