简体   繁体   中英

How to deal with [-Wc++11-extensions] in the text editor

I am experiencing a warning when I try to initialize myCurrentTry to 1 and myMaxTries to 5, the error is:

in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]

The code that is making this happen is:

class starUFO{
public:
    void Reset();
    int getMaxTries();
    int getCurrentTry();
    bool isGameWon();
    bool checkGuessValidity(string);
private:
    int myCurrentTry = 1;
    int myMaxTries = 5;
};

Ive heard people say "try compiling with -std=c++11" and this does take away the warning in my command line BUT I want to fix the VS Code configuration so it doesn't see this as a warning, I have the latest VS Code but there seems to be a deeper issue.

Write a constructor and initialize your member variables there :

class starUFO
{
    private:
        int myCurrentTry;
        int myMaxTries;

    public:
        starUFO()
        :   myCurrentTry(1),
            myMaxTries(5)
        {
        }

        ...
};

To tell VSCode which C++ standard you are using, open the Command Palette (Ctrl+Shift+P), choose "C/C++: Edit Configurations (UI)", scroll down and change the "C++ standard" dropdown. In this case, choose "c++11" or higher.

This assumes you are using the "IntelliSense" engine, rather than the older "Tag Parser" engine: go to File → Preferences → Settings → C/C++ → "C_Cpp: Intelli Sense Engine", and make sure it is set to "Default".

These options exist in VSCode 1.37.1 and C/C++ extension 0.25.1 (aka "cpptools"). I don't know when they were introduced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM