【问题标题】:‘operator/’ is not a member of ‘std::filesystem’; did you mean ‘operator~'‘operator/’不是‘std::filesystem’的成员;你的意思是“操作员~”
【发布时间】:2021-09-24 04:14:12
【问题描述】:

我正在尝试安装 MMMTools https://mmm.humanoids.kit.edu/installation.html。我的 cmake 版本是 3.16.3。在本节之前,我完成了每一步,没有任何错误

cd ~/MMMCore
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make   

make 命令返回以下错误。

(base) kong@kong-Standard:~/MMMCore/build$ make
[  1%] Building CXX object CMakeFiles/MMMCore.dir/MMM/XMLTools.cpp.o
/home/kong/MMMCore/MMM/XMLTools.cpp: In function ‘void MMM::XML::makeAbsolutePath(const string&, std::string&)’:
/home/kong/MMMCore/MMM/XMLTools.cpp:650:64: error: ‘operator/’ is not a member of ‘std::filesystem’; did you mean ‘operator~’?
  650 |   std::filesystem::path filenameNewComplete = std::filesystem::operator/(filenameBasePath, filenameNew);
      |                                                                ^~~~~~~~~
      |                                                                operator~
make[2]: *** [CMakeFiles/MMMCore.dir/build.make:76: CMakeFiles/MMMCore.dir/MMM/XMLTools.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:295: CMakeFiles/MMMCore.dir/all] Error 2
make: *** [Makefile:163: all] Error 2

但是我用谷歌搜索了这个函数,发现它是 std::filesystem https://en.cppreference.com/w/cpp/filesystem/path/operator_slash 的成员。出了什么问题?

我浏览了 CMakeLists.txt 并看到了这个。

###########################################################
#### Compiler configuration                            ####
###########################################################

set(CMAKE_POSITION_INDEPENDENT_CODE ON) # enable -fPIC

if(MSVC)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /MP")
elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
    # cmake 3.10 does not understand c++2a, so we tell it we will handle the standard flag
    set(CMAKE_CXX_STANDARD_DEFAULT)
    add_definitions(-std=c++2a)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -pedantic")
endif()

add_compile_options(-Werror)

我在 Ubuntu 20.04 上编译它,所以我猜它进入了 elseif 部分。这是否意味着它没有使用 C++17?我是否需要编辑 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -pedantic") 行以使其使用 C++17?

该指南指出它适用于 18.04,但由于问题出在 C++17,我认为问题不在于使用 20.04?

【问题讨论】:

  • operator/ 自 C++17 起是 std::filesystem 的成员。 makefile 指定了什么标准?
  • @Chris 我已经更新了我的帖子以包含 CMakeLists.txt 中指定编译器的部分。我需要将其更改为 C++17 吗?我是 CMake 的新手。
  • 拼写 std::filesystem::operator/(filenameBasePath, filenameNew) 违背了 operator/ 的目的。一个应该使用filenameBasePath/filenameNew。只是说。

标签: c++ makefile cmake std-filesystem


【解决方案1】:

由于LWG 3065,该运算符现在被隐藏,不应直接调用。

std::filesystem::path filenameNewComplete = std::filesystem::operator/(filenameBasePath, filenameNew);

应该是:

std::filesystem::path filenameNewComplete = filenameBasePath / filenameNew;

我猜代码只是针对比您使用的更旧的实现(看起来这是在 gcc/libstdc++ 9 中实现的)进行了测试,我不知道为什么它写得这么复杂不过最初的方式。

【讨论】:

  • 您显示了std::filesystem::operator/ 的声明,它显然是std::filesystem 的成员。它返回std::filesystem::path,这是一个类型。
  • @n.1.8e9-where's-my-sharem。固定
猜你喜欢
  • 1970-01-01
  • 2012-09-29
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多