V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
coool
V2EX  ›  C

C++ 原生字符串编译报错如何处理?

  •  
  •   coool · 2018-09-13 12:43:09 +08:00 · 2195 次点击
    这是一个创建于 2051 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如图,请问如何解决? 代码:

    #include <iostream> #include <string> using namespace std;</string></iostream>

    int main() { cout << R "(C:\file)";

    return 0;
    

    }

    报错: 2-1-1.cpp: In function ‘ int main()’: 2-1-1.cpp:7:13: error: ‘ R ’ was not declared in this scope cout << R "(C:\file)";

    6 条回复    2018-09-16 06:36:05 +08:00
    wevsty
        1
    wevsty  
       2018-09-13 12:54:32 +08:00
    什么编译器?
    R 这种原始字符串语法只有支持 C++11 以上标准的编译器才能使用。

    MSVC 必须得 2010 以上版本。

    g++需要添加-std=c++11 这样的参数才支持的
    wevsty
        2
    wevsty  
       2018-09-13 12:56:57 +08:00
    补充一下
    https://msdn.microsoft.com/en-us/library/hh567368.aspx
    如果是 MSVC 的话根据 MSDN
    raw string 这个语法必须得是 Visual Studio 2013 以上才能支持。
    chingyat
        3
    chingyat  
       2018-09-13 15:42:01 +08:00   ❤️ 1
    R 和 “ 之间不要加空格。
    coool
        4
    coool  
    OP
       2018-09-13 16:02:57 +08:00
    @chingyat 您好,非常感谢,确实是不能加空格,另外我用的楼上的 g++,增加了 -std=c++11 编译成功了。
    coool
        5
    coool  
    OP
       2018-09-13 16:04:08 +08:00
    @wevsty 您好,非常感谢指导!我用的 g++,增加了-std=c++11 参数另外去掉了 R 和引号之间的空格,编译成功了。谢谢。
    FrankHB
        6
    FrankHB  
       2018-09-16 06:36:05 +08:00
    你用的什么古董 g++,g++6 开始默认-std=gnu++14。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1942 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:22 · PVG 00:22 · LAX 09:22 · JFK 12:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.