C ++ 14 и код Visual Studio - PullRequest
       5

C ++ 14 и код Visual Studio

0 голосов
/ 01 мая 2018

У меня проблема с векторной инициализацией.

Когда я пытаюсь запустить этот код:

#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
}

Файл My 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, решение1 и решение2 но все равно не работает.

Как я могу это исправить ??

1 Ответ

0 голосов
/ 13 мая 2018

Я никогда не видел, чтобы std :: vector инициализировался подобным образом, обычно я делал бы что-то вроде std::vector<int> a = std::vector<int>({5, 5, 5});.

РЕДАКТИРОВАТЬ: Вы даже можете сделать std::vector<int> a = {5, 5, 5};.

...