error C2679: 二进制“<<”: 没有找到接受 'std::string_view' 类型右手操作数的运算符(或者没有可接受的转换)

error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::string_view' (or there is no acceptable conversion)

#include <iostream>
#include <string>

void Log(std::string_view message)
{
    std::cout << message << std::endl;
}

int main()
{
    const char* text = "Test";
    Log(text);
    std::cin.get();
}

我遇到了那个错误,#include <string> 包含在开头。有什么想法吗?

就像已经提到的其他人一样 - std::string_view 在标准 header“string_view”中定义,必须包括在内 - 否则 string_view 未定义。

因为您包含了一些 header 与 std::string_view 有某种联系的“字符串”和“iostream”,很明显它们至少做了一些 forward_declarations。

在某些实现中 string_view 可能已经包含在其他系统 header 中。 例如 std::string_view 可以是 header“字符串”中的 implemented/defined,而 header“string_view”可以只包含“字符串”。

但总的来说,这是库实现的一个实现细节。为了能够使用 std::string_view,需要包含 header.