简体   繁体   中英

Why do I get a linter error when I include std::vector?

This is a problem that seem to occur from time to time for me. The linter says that "namespace std has no member 'vector'" although the program compiles and runs fine. The only problem besides the annoyance is that I cannot use the ctrl-click feature on the GameObject-class. The linter (intellisense) does not find it. I have tried to run a clean solution and recompile, as well as delete the temp-folder and the hidden.vs- file to no avail.

#include "GameObject.h"
#include "GraphManager.h"
#include <vector>

class GameObjectManager
{
    static std::vector<GameObject> myGameObjects; 

    void CheckCollisions();
    static int myIDCount;


public:
    GameObjectManager() = default; 
    static void Init();
    static GameObject& GetGameObject(const int index) { return myGameObjects.at(index); };

    static void Update(const float aDeltaTime);

    static void Render(); 
};

enter image description here

Which linter are you using?

Basically a linter tries to "compile" - meaning to look at - your source code pretty much the same way the compiler does. But for this to work, the C++ "environment" - meaning: the include paths, predefined macros and compiler options - must be (nearly) the same as the ones the compiler uses.

For example: If you compile your code in "release" configuration, several predefined and "by-convention" macros are set, such as NDEBUG, _WIN32 and others. Other macros such as _DEBUG are not set.

The microsoft std::vector implementation depends heavily on such macros. Depending on the macros it enables or disables features such as different levels of iterator validation in debug builds etc...

When you use a (non-microsoft) linter and run "lint.exe" (or whatever it is), you must make sure (define on the linter command line), that "lint.exe" is started with the same set of macros, include paths etc... as the compiler cl.exe.

If that is not the case, the missing macros may well cause the linter to miss the entire std::vector definition.

Or - alternatively - the linter is not good enough to untangle the internal web of macros. The namespace std { } statement for example might not be written verbatim in <vector> , but might require macro expansion and possibly a lot of it. If the linter has problems doing that, vector might be defined somewhere, just not in a std namespace.

C++ is notoriously hard to parse by tools.

Which is why I would recommend using Microsofts "Code Analysis" Tool, since you are using the Microsoft Compiler anyway.

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