为什么我在尝试使用库打开文件时收到字符串的 C2440 错误<netcdf>?



我正在尝试用c++库读取netCDF文件。但是,我得到了一个C2440错误,告诉我不能将数据文件的类型转换为数据文件。或者,至少这是我认为它告诉我的,因为编译器错误看起来有点晦涩难懂。

下面是我的代码:
//create a test object to open netcdf file 
const std::vector<std::string> path = obj.getPaths(); //returns all paths in the netcdf directory
const std::string test = path[0]; //gives me a test path that is element 1 in the path vector
netCDF::NcFile dataFile(test, netCDF::NcFile::read);
netCDF::NcVar dataVar = dataFile.getVars();

错误:

Error C2440 'initializing': cannot convert from 'std::multimap<std::string,netCDF::NcVar,std::less<Aws::String>,std::allocator<std::pair<conststd::string,netCDF::NcVar>>>' to 'netCDF::NcVar' count-lightning C:UsersCorey4005DesktopDevprojectscount-lightningsrcmain.cpp 59  

我应该怎么做才能使这段代码工作?

谢谢你的帮助!

我已经试过了:

我试图使用NcFile流打开netcdf文件。NcFile流接受以下参数:

netCDF::NcFile dataFile(const std::string &filePath, netCDF::NcFile::fileMode fMode); 

当我向filePath参数提供字符串文字时,我收到了C2440"初始化"错误。我很困惑,因为我认为我正确地将const字符串文字传递给const字符串参数。

netCDF::NcVar dataVar是单个变量,dataFile.getVars()返回映射中的多个变量。最简单的修复方法是使用:

auto dataVar = dataFile.getVars();

或:

std::multimap< std::string, NcVar > dataVar = dataFile.getVars();

如果你想获得一个单独的变量,你需要提供一个名称:

netCDF::NcVar dataVar = dataFile.getVar("myvar");

见http://unidata.github.io/netcdf-cxx4/classnetCDF_1_1NcFile.html

最新更新