【问题标题】:Intellisense error when passing vector by const reference (but project builds)通过 const 引用传递向量时出现 Intellisense 错误(但项目构建)
【发布时间】:2020-02-16 18:18:57
【问题描述】:

在尝试通过 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;
}

【问题讨论】:

  • 请将您的问题简化为minimal reproducible example。我自己无法测试您现在显示的内容,因为代码不完整并且我没有看到任何明显的问题(或者我忽略了它),可能是因为不相关的代码太多。
  • vertex.htexture.h 标头可能存在问题,因此应将它们添加到问题中。还要检查并确保在不同的目录中没有这些标题的多个副本。
  • @walnut 我已按要求用minimal reproducible example 更新了问题。很抱歉没有从一开始就这样做。
  • @whitwhoa 我无法在 GCC、Clang 或 MSVC 上使用您的代码重现该错误,请参阅 godbolt.org/z/P88oNz。您正在使用哪些编译器和编译器选项?
  • @walnut 我正在通过 Visual Studio 2017 使用 MSVC。正在使用的唯一选项是 /permissive- /GS,我相信它是默认启用的,/std:c++ latest

标签: c++ reference visual-studio-2017 constants intellisense


【解决方案1】:

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

我已经在我最新的 VS2017 中测试了您的示例,并且 Intellisense 运行良好,符合预期。所以我想知道您的 VS 环境和 Intellisnese 存在一些问题。

我的环境-- 操作系统:win10 1903 VS:VS2017 Community 19.9.20

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

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

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

2)清理C:\Users\user\AppData\Local\Microsoft\VisualStudio\15.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 损坏。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 2019-08-08
    • 2014-08-22
    • 2017-03-14
    • 1970-01-01
    相关资源
    最近更新 更多