Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ compile error 'expected ';' at end of declaration' when using direct brace initialization

I'm very new to C++, working through my first tutorial, and when I try to compile code from the lesson, I get the following error:

expected ';' at end of declaration
    int x{ }; // define variable x to hold user input (a...
         ^
         ;

The full code for the program I'm attempting to run:

#include <iostream>  // for std::cout and std::cin
 
int main()
{
    std::cout << "Enter a number: ";
    int x{ }; 
    std::cin >> x; 
    std::cout << "You entered " << x << '\n';
    return 0;
}

I am using Visual Studio Code (v.1.46.1) on a Macbook Pro, with the Microsoft C/C++ extension (https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools).

My compiler is Clang:

Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Initially, I ran Terminal > Configure Default Build Task in VS Code to create a .vscode/tasks.json compiler settings file. That file currently looks like this:

{
    "version": "2.0.0",
    "tasks": [
    {
      "type": "shell",
      "label": "C/C++: clang++ build active file",
      "command": "/usr/bin/clang++",
      "args": [
        // Set C++ Standards 
        "-std=c++17",

        // Increase compiler warnings to maximum
        "-Wall",
        "-Weffc++",
        "-Wextra",
        "-Wsign-conversion",

        // Treat all warnings as errors
        "-Werror",

        // Disable compiler extensions
        "-pedantic-errors",

        // File to compile
        "-g",
        "${file}",

        // Output file
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

I have the -std=c++17 flag set, which should allow direct brace initialization from what I understand.

I'm not sure it matters, since I'm trying to compile and not build/debug, but for the sake of thoroughness, I also have a .vscode/launch.json file with the following contents:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "clang++ - Build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": true,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "lldb",
      "preLaunchTask": "C/C++: clang++ build active file"
    }
  ]
}

Can someone help me figure out why int x{ }; is not working properly to intitialize the variable and what I can do to fix it so it will work?

[Edit]: Further settings I've checked/tested:

  • Code compiles correctly when running compile directly from command line with clang++ -std=c++17 -g helloworld.cpp -o helloworld
  • VS Code C/C++ extension configuration has setting 'C++ standard' set to c++17 (seems to be the default). Even so, running command-line compile without -std=c++17 flag set causes same compiler error.
  • Tried changing int x{ }; to the following:
    • int x( );: fails with a very long list of errors
    • int x(0);: compiles successfully
    • int x = { };: compiles successfully
    • int x = {0};: compiles successfully
    • `int x;': compiles successfully
    • `int x = 0;': compiles successfully
like image 882
M. Layton Avatar asked Jun 20 '20 15:06

M. Layton


2 Answers

I had the same issue and there was an error in my tasks.json. This worked for me, try it:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
      {
        "type": "shell",
        "label": "clang++ build active file",
        "command": "/usr/bin/clang++",
        "args": [
          "-std=c++17",
          "-stdlib=libc++",
          "-g",
          "${file}",
          "-o",
          "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
          "cwd": "${workspaceFolder}"
        },
        "problemMatcher": ["$gcc"],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
  }
like image 108
Greg Coleson Avatar answered Nov 15 '22 19:11

Greg Coleson


I had exactly the same problem, I tried to modify tasks.json, c_cpp_properties.json and settings.json but nothing worked. I finally tried the solution shown here, which is to disable the C/C++ Clang Command Adapter extension, and it worked for me!

like image 1
Romain Simon Avatar answered Nov 15 '22 17:11

Romain Simon