3

I'm having a problem with vector initialization.

When I try to run this code:

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

I keep getting this error:

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

I'm trying to figure out to run c++11 or c++14 but I'm lost at the moment.

My c_cpp_properties.json file:

{
    "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 file:

{
    // 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
            }
        }
    ]
}

I've tried to solve this issue by following ways from github, solution1 and solution2 but still doesn't work.

How can I fix this??

Lydia
  • 81
  • 3
  • 1
    [This question](https://stackoverflow.com/questions/2236197/what-is-the-easiest-way-to-initialize-a-stdvector-with-hardcoded-elements) and related answers should help. [This answer](https://stackoverflow.com/a/44809186/272109), specifically, shows a similar vector initialization, implying that your issue is a missing `=` (e.g. change your code to `vector a = {5, 5, 5} ; ` ) – David Makogon May 01 '18 at 12:31
  • 1
    Cannot reproduce compiling C++11 or C++14 with g++ or clang++. Can you see the exact compiling command that generate the error? Are you sure you're compiling C++11 or C++14? – max66 May 01 '18 at 12:40
  • Your `tasks.json` file refers to a different file (`"vectoroutofbounds2.cpp"`) than your error message (`"tempCodeRunnerFile.cpp"`) - also please add the terminal output of the task run (should be starting with "Executing task:") – UnholySheep May 01 '18 at 12:56
  • 3
    @DavidMakogon you are wrong, OP's code is completely valid (see here: https://ideone.com/Hr7nc7) there is no missing `=`. [Direct List initialization](http://en.cppreference.com/w/cpp/language/list_initialization) is a C++11 feature, meaning the compiler is not compiling with these features enabled (which is exactly what the question is about) – UnholySheep May 01 '18 at 13:02
  • @UnholySheep The file name is "vectoroutofbounds2.cpp" so I'm also confused why "tempCodeRunnerFile.cpp" comes up as an error. For the last part, how can I do that?? – Lydia May 01 '18 at 13:51
  • There should be more text above the error message you posted - somewhere at the beginning there should be the actual command that was executed by the task (on my machine it looks like **> Executing task: clang++ -g test.cpp <** - the part after "Executing task" is important here) – UnholySheep May 01 '18 at 13:57
  • @UnholySheepcd Now I'm getting: "/Users/~/" && g++ vectoroutofbounds2.cpp -o vectoroutofbounds2 && "/Users/~"vectoroutofbounds2 vectoroutofbounds2.cpp:8:18: error: expected ';' at end of declaration vector a{5, 5, 5} ; ^ ; 1 error generated. – Lydia May 01 '18 at 15:55
  • Well that command is completely different than what you have in your `tasks.json` file - are you absolutely sure that the one you posted is the correct one (and you don't have a different one in the `.vscode` folder)? If yes I would suggest trying to delete the tasks file and creating it again – UnholySheep May 01 '18 at 16:12
  • @UnholySheep Yes, there is only one tasks.json file which I typed above. I think I have previously written it including something like "-Wall", "&&" and stuff but right now it is just like what I've posted. This is driving me nuts. – Lydia May 01 '18 at 16:19
  • Unfortunately I am not able to reproduce your problem. Deleting the `tasks.json` file and creating it again (via VS Code) is the only thing I could think of that could still work. Something is definitely odd, but I don't think this can be diagnosed remotely as it only occurs on your PC – UnholySheep May 01 '18 at 16:25

1 Answers1

-2

I've never seen an std::vector be initialised like that before, usually I would do something like std::vector<int> a = std::vector<int>({5, 5, 5});.

EDIT: You can even do std::vector<int> a = {5, 5, 5};.

Victor
  • 32
  • 6
  • You never seen is only **you** never seen. direct-list-initialize is different from your two codes, especially the first code you used copid a vector. These are the reason why I downvoted. – con ko May 13 '18 at 01:39