通过 const 引用传递向量时出现 Intellisense 错误(但项目构建)

Intellisense error when passing vector by const reference (but project builds)

在尝试通过 const 引用传递向量时,我收到以下智能感知错误(项目构建成功):

no instance of constructor "ObjOne::ObjOne" matches the argument list
argument types are: (std::vector<int, std::allocator<int>>) 

我在下面的最小可重现示例中评论了发生错误的地方。在 visual studio 2017 年内是否有解决此类问题的已知方法?

#include <vector>
#include <string>
#include <optional>

class ObjOne
{
public:
    ObjOne(const std::vector<int>& p1) {}
};

class ObjTwo
{
private:
    std::vector<int> testVec = { 1,2,3,4,5 };
    std::optional<ObjOne> optObjOne;
public:
    ObjTwo() {}
    void makeObjOne() 
    {
        this->optObjOne = ObjOne(this->testVec); // Issue arises here
    }
};


int main()
{
    auto myObjTwo = ObjTwo();
    myObjTwo.makeObjOne();

    return 0;
}

Intellisense error when passing vector by const reference (but project builds)

我已经在我最新的 VS2017 中测试了你的示例,Intellisense 的效果符合预期。所以我想知道你的VS环境和Intellisnese有什么问题。

我的环境-- OS: win10 1903 VS:VS2017 Community 19.9.20

您可以尝试我的建议来解决您的问题:

作为建议,如果您的 VS2017 不是最新版本,您可以将其更新到最新版本,该版本可能有较新的修复。

1)关闭VS实例,删除.vs隐藏文件夹、bin文件夹和obj文件夹

2)清理C:\Users\user\AppData\Local\Microsoft\VisualStudio.0_xxxxx\ComponentModelCache

下的VS组件缓存

3)在VS中打开你的项目并右击你的项目-->Properties-->C/C++-->Language-->设置C++ Language StandardISO C++ 17 Standard(/std:c++17)

此外,如果这些都不起作用,请尝试在 VS Installer 中do a repair,以防 Intellisense 损坏。

希望对您有所帮助。