0

I am using a specific syntax needed for a course, but when I use this C++ syntax in VS Code, it doesn't work and raises errors.

Here is an example of the syntax that is not working:

error: expected ';' at end of declaration
        int i {0}; 
             ^
             ;

When I change it to int i = 0; the error disappears.

It specifically doesn't recognize the {} syntax for setting default variable values. I am using a ssh login for this course and the syntax works well in the ssh, but won't work in VS Code.

I attempted to change my VS Code C++ version to C++17 by doing the top answer in this thread, but it still doesn't recognize the syntax.

Am I using incorrect syntax, or is there a way to fix this?

6
  • The syntax is fine, you are likely just not telling the compiler to use C++11 or later correctly (what does __cplusplus report in the code?). Commented Aug 9, 2022 at 0:05
  • @RemyLebeau I'm a bit new to C++, can you explain how I can run/use that command? Do I just write it in the code and compile+run it? Do I execute the command in Terminal? Commented Aug 9, 2022 at 0:09
  • @RemyLebeau When I cout << __cplusplus;, it outputs: 199711. When I hover over __cplusplus in VS Code with my mouse, it shows #define __cplusplus 201703L Expands to: 201703L Commented Aug 9, 2022 at 0:15
  • Then clearly you are compiling in C++98 mode, which is why the syntax does not work, since C++98 predates C++11. VS Code is an editor, not a compiler. Which exact compiler are you using with it? Which exact settings did you change to try to enable C++11 or later? Be specific. Commented Aug 9, 2022 at 0:18
  • 1
    My guess is macOS (since the compiler defaults to c++98). The VSCode documentation explains the 2 places the standard needs to be set here: https://code.visualstudio.com/docs/cpp/config-clang-mac Commented Aug 9, 2022 at 0:21

1 Answer 1

5

Adding on to the comment above, this is what solved my issue:

Go to this link: https://code.visualstudio.com/docs/cpp/config-clang-mac

Go to the section Clang on macOS and scroll down to Troubleshooting. Follow the steps in this paragraph:

"If you see build errors mentioning "C++11 extensions", you may not have updated your tasks.json build task to use the clang++ argument --std=c++17. By default, clang++ uses the C++98 standard, which doesn't support the initialization used in helloworld.cpp. Make sure to replace the entire contents of your tasks.json file with the code block provided in the Run helloworld.cpp section."

You will need to copy-paste this exact code and replace the code in the tasks.json folder:

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "C/C++: 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
      },
      "detail": "Task generated by Debugger."
    }
  ]
}

To find the tasks.json folder, go to your program-file, do Command + Shift + . to find hidden files > .vscode > tasks.json > replace all the code in the file with the one I provided. It updates the C++ compiler to use a more updated version of C++ which recognizes the syntax.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.