创建 string_view 元素数组会引发错误:无法找到字符串文字运算符 'operator""sv' with

Nav*_*ore 3 c++ string-view c++17

我有以下(修改后的)代码,我想在其中创建对象数组std::string_view

\n

对应每一行编译时我看到这个错误

\n
unable to find string literal operator \xe2\x80\x98operator""sv\xe2\x80\x99 with \xe2\x80\x98const char [8]\xe2\x80\x99, \xe2\x80\x98long unsigned int\xe2\x80\x99 arguments\n         "Sensor2"sv,\n
Run Code Online (Sandbox Code Playgroud)\n

代码:

\n
#include <iostream>\n#include <array>\n#include <string_view>\n\nstruct Abc\n{\n    static constexpr std::array<std::string_view, 6> SomeValues = {\n        "Sensor1"sv,\n        "Sensor2"sv,\n        "Actuator1"sv,\n        "Actuator2"sv,\n        "Cpu1"sv,\n        "Cpu2"sv\n    };\n    \n};\n\n\nint main()\n{\n    Abc abc;\n    \n    std::cout << abc.SomeValues[3];\n\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Enr*_*lis 12

你需要using namespace std::literals;,这没关系,不像using namespace std;

另请参阅此处 StackOverflow 上的这个问题,以及最重要的是,来自 C++ 核心指南 的此项