我试图将std::shared_ptr静态转换为它的基类。给定的类:

class ImportFileSetting {};
class ImportFileSettingSelect:ImportFileSetting {}


我尝试了以下代码:

std::shared_ptr<ImportFileSettingSelect> selectedSheet_ = std::make_shared<ImportFileSettingSelect>();
std::vector<std::shared_ptr<ImportFileSetting>> settings_;
settings_.push_back(static_pointer_cast<ImportFileSetting>(selectedSheet_));


我根据cppreference.com找到的。但是它无法编译为:

5>src\TechAdminServices\database\techCore\processes\ImportDataSourceExcel.cpp(32): error C2065: 'static_pointer_cast' : undeclared identifier
5>src\TechAdminServices\database\techCore\processes\ImportDataSourceExcel.cpp(32): error C2275: 'ImportFileSetting' : illegal use of this type as an expression
5>          d:\techsys\techadmin\techadminservices\src\techadminservices\database\techcore\processes\ImportFileSetting.h(7) : see declaration of 'ImportFileSetting'


我该如何解决?

最佳答案

您缺少名称空间:

settings_.push_back(std::static_pointer_cast<ImportFileSetting>(selectedSheet_));
//                  ^^^^^

10-02 01:44