duplicate designator is not allowedC/C++(2906)

系统:ubuntu 20.04
工具:visual studio code ver 1.67.2
错误发生原因:结构体成员指定初始化(Designated initializers)出错
错误场景还原:


#include "stdio.h"
struct test{
    int ID;
    int name; 
};
 
int main(void)
{
    struct test t1 = {1,2};
    
    struct test t2 = {3,4};
    /* t3 在vscode中会报错duplicate designator is not allowedC/C++(2906)*/
    struct test t3 ={
     .ID = 2019, 
     .name = 1111};    
 }

错误分析:
应该是vscode C++ Intellisense 没有完全支持 gcc编译器的问题,虽然并不影响编译,但是实在是闹心,查到github上的回复是已经在1.4.0-insiders版本应该已经修复了,官方已经到v1.9.8版本,但是依然有这个问题,即使手动安装1.4.0版本错误提示依然存在。
在这里插入图片描述
解决方法:将编译器设置改为clang,我的c_cpp_properties.json配置如下:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}/src/**",
                "/usr/lib/clang/10.0.0/include/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++20",
            "intelliSenseMode": "clang-x64",
            "configurationProvider": "ms-vscode.makefile-tools"
        }
    ],
    "version": 4
}

"includePath"中加入clang的头文件库,并且在compilePath中设置clang的位置就可以了,具体位置查询可以使用which clang获得。

切换clang后vsc可能出现的问题:

1、使用#define来定义标识符来表示一个字符串常量,如果在使用过程中出现拼接,字符串和标识符之间需要增加空格,否则会报错:user-defined literal operator not found C/C++(2486)

输出错误:
在这里插入图片描述
正确写法:
在这里插入图片描述

Ref:
https://github.com/microsoft/vscode-cpptools/issues/6316
https://github.com/microsoft/vscode-cpptools/issues/3491
https://github.com/microsoft/vscode-cpptools/issues/5550

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值