【问题标题】:C++14 and Visual Studio CodeC++14 和 Visual Studio 代码
【发布时间】:2018-10-11 11:47:00
【问题描述】:

我遇到了向量初始化问题。

当我尝试运行这段代码时:

#include <iostream>
#include <vector>

using namespace std;

int main() {
    const int SIZE = 3;
    vector<int> a{5, 5, 5} ;

    cout << "a contains ";
    for (int i = 0; i < SIZE; i++)
        cout << a.at(i) << ' ';
    cout << '\n';

    for (int i = 0; i < SIZE; i++)
        a.at(i) = 8;

    cout << "a contains ";
    for (int i = 0; i < SIZE; i++)
        cout << a.at(i) << ' ';
    cout << '\n';
}

我不断收到此错误:

tempCodeRunnerFile.cpp:8:18: error: expected ';' at end of declaration
    vector<int> a{5, 5, 5} ;
                 ^
                 ;
1 error generated.

我正在尝试找出运行 c++11 或 c++14,但我现在迷路了。

我的 c_cpp_properties.json 文件:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "/usr/include",
                "/usr/local/include",
                "${workspaceRoot}",
                "/usr/include/c++/4.2.1",
                "/usr/include/c++/4.2.1/tr1"
            ],
            "defines": [],
            "intelliSenseMode": "clang-x64",
            "browse": {
                "path": [
                    "/usr/include",
                    "/usr/local/include",
                    "${workspaceRoot}"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            },
            "macFrameworkPath": [],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17"
        },
        {
            "name": "Linux",
            "includePath": [
                "/usr/include",
                "/usr/local/include",
                "${workspaceRoot}"
            ],
            "defines": [],
            "intelliSenseMode": "clang-x64",
            "browse": {
                "path": [
                    "/usr/include",
                    "/usr/local/include",
                    "${workspaceRoot}"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        },
        {
            "name": "Win32",
            "includePath": [
                "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include",
                "${workspaceRoot}"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "path": [
                    "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include/*",
                    "${workspaceRoot}"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ],
    "version": 3
}

我的 tasks.json 文件:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "g++",
            "args": [
                "-o", "test", "-std=c++14", "vectoroutofbounds2.cpp"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

我尝试通过 github 中的方法解决此问题, solution1solution2 但还是不行。

我该如何解决这个问题??

【问题讨论】:

  • This question 和相关答案应该会有所帮助。 This answer,具体来说,显示了类似的向量初始化,暗示您的问题是缺少=(例如,将您的代码更改为vector&lt;int&gt; a = {5, 5, 5} ;
  • 无法重现使用 g++ 或 clang++ 编译 C++11 或 C++14。你能看到产生错误的确切编译命令吗?你确定你是在编译 C++11 还是 C++14?
  • 您的 tasks.json 文件与您的错误消息 ("tempCodeRunnerFile.cpp") 指向不同的文件 ("vectoroutofbounds2.cpp") - 还请添加任务运行的终端输出(应以“正在执行任务:")
  • @DavidMakogon 你错了,OP 的代码是完全有效的(见这里:ideone.com/Hr7nc7)没有缺少=Direct List initialization 是 C++11 功能,这意味着编译器不会在启用这些功能的情况下进行编译(这正是问题所在)
  • @UnholySheep 文件名是“vectoroutofbounds2.cpp”,所以我也很困惑为什么会出现“tempCodeRunnerFile.cpp”作为错误。对于最后一部分,我该怎么做??

标签: c++ c++11 visual-studio-code c++14


【解决方案1】:

我以前从未见过像这样初始化 std::vector,通常我会做类似std::vector&lt;int&gt; a = std::vector&lt;int&gt;({5, 5, 5}); 之类的事情。

编辑:你甚至可以std::vector&lt;int&gt; a = {5, 5, 5};

【讨论】:

  • 你从未见过只是从未见过。 direct-list-initialize 与您的两个代码不同,尤其是您使用的第一个代码 copid 一个向量。这就是我投反对票的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-27
相关资源
最近更新 更多