c++ - 两个具有相同代码的代码库,但一个正在生成重载解析编译错误,而另一个则没有

标签 c++ compiler-errors visual-studio-2017 vulkan overload-resolution

我正在关注 Jonno Robson 的 Vulkan 应用程序源代码,可以在 github 上找到:Vulkan-Terrain-Generator .我在 Windows 7 64 位机器上使用 Visual Studio 2017。

我有自己的解决方案和项目,其中所有内容都是手动输入的,但我也有他们项目的下载克隆,以便我可以并排使用它来尝试学习和更好地理解 Vulkan API。

Renderer 类的 initPipelines() 中功能我们正在为渲染场景设置所有不同的管道。在我们设置TerrainRenderingPipeline 的部分,调用了addTextureArray()。继承类从其基类成员调用。 TerrainRenderingPipeline 类继承自 Pipeline 基类。

这就是它在 Render 类的 initPipelines() 中的调用方式方法:

terrain_rendering_pipeline_->addTextureArray(VK_SHADER_STAGE_VERTEX_BIT, 3, terrain_generator_->getHeightmaps());

在我的解决方案中;这无法编译并且 Visual Studio 正在生成 C2664提示重载决议的错误。然而;当我编译和构建 Jonno 的项目时,没有任何问题。


这些是 addTextureArray(...) 的函数声明功能 在 pipeline.h 中找到:

void addTextureArray(VkShaderStageFlags stage_flags, uint32_t binding_location, std::vector<Texture*>& textures);
void addTextureArray(VkShaderStageFlags stage_flags, uint32_t binding_location, std::vector<VkImageView>& textures);

这些是在 pipeline.cpp 中找到的定义

void VulkanPipeline::addTextureArray(VkShaderStageFlags stage_flags, uint32_t binding_location, std::vector<Texture*>& textures) {
    Descriptor texture_descriptor = {};

    // setup image info
    for (Texture* texture : textures) {
        VkDescriptorImageInfo image_info = {};
        image_info.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
        image_info.imageView = texture->getImageView();
        image_info.sampler = VK_NULL_HANDLE;
        texture_descriptor.image_infos.push_back(image_info);
    }

    // setup descriptor layout info
    texture_descriptor.layout_binding.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
    texture_descriptor.layout_binding.descriptorCount = texture_descriptor.image_infos.size();
    texture_descriptor.layout_binding.binding = binding_location;
    texture_descriptor.layout_binding.stageFlags = stage_flags;
    texture_descriptor.layout_binding.pImmutableSamplers = nullptr;

    descriptor_infos_.push_back(texture_descriptor);
}

void VulkanPipeline::addTextureArray(VkShaderStageFlags stage_flags, uint32_t binding_location, std::vector<VkImageView>& textures) {
    Descriptor texture_descriptor = {};

    // setup image info
    for (VkImageView texture : textures) {
        VkDescriptorImageInfo image_info = {};
        image_info.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
        image_info.imageView = texture;
        image_info.sampler = VK_NULL_HANDLE;
        texture_descriptor.image_infos.push_back(image_info);
    }

    // setup descriptor layout info
    texture_descriptor.layout_binding.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
    texture_descriptor.layout_binding.descriptorCount = texture_descriptor.image_infos.size();
    texture_descriptor.layout_binding.binding = binding_location;
    texture_descriptor.layout_binding.stageFlags = stage_flags;
    texture_descriptor.layout_binding.pImmutableSamplers = nullptr;

    descriptor_infos_.push_back(texture_descriptor);
}

最后是TerrainGenerator 类的getHeightmaps()返回 std::vector<VkImageView> 的函数在 terrain_generator.h 中找到:

inline std::vector<VkImageView> getHeightmaps() { return heightmap_image_views_; }

我相信这是涉及此问题的所有相关代码。


这是生成的 Visual Studio 编译器错误:

1>c:\users\skilz99\source\repos\vulkan terrain generator\vulkan terrain generator\renderer.cpp(522): error C2664: 'void VulkanPipeline::addTextureArray(VkShaderStageFlags,uint32_t,std::vector<VkImageView,std::allocator<_Ty>> &)': cannot convert argument 3 from 'std::vector<VkImageView,std::allocator<_Ty>>' to 'std::vector<Texture *,std::allocator<_Ty>> &'
1>        with
1>        [
1>            _Ty=VkImageView
1>        ]
1>        and
1>        [
1>            _Ty=VkImageView
1>        ]
1>        and
1>        [
1>            _Ty=Texture *
1>        ]
1>c:\users\skilz99\source\repos\vulkan terrain generator\vulkan terrain generator\renderer.cpp(545): warning C4305: 'argument': truncation from 'double' to 'T'
1>        with
1>        [
1>            T=float
1>        ]

我不明白的是为什么它无法在我的项目解决方案中编译,但在他的...中却没有...任何类型的提示或建议都会非常有帮助。如果您需要有关代码的更多信息,请随时询问。我尝试发布尽可能少的代码,因为这是一个体面的项目。

最佳答案

当您在调用 addTextureArray 时使用 getHeightmaps 函数时,它返回的对象是一个临时 对象。临时对象不能绑定(bind)到非常量引用。 addTextureArray 为 vector 采用非常量引用参数。

必须修改 addTextureArray 函数以接受 const 引用作为第三个参数:

void addTextureArray(VkShaderStageFlags, uint32_t, std::vector<Texture*> const&);
void addTextureArray(VkShaderStageFlags, uint32_t, std::vector<VkImageView> const&);

或者您必须修改getHeightmaps 函数以返回一个引用:

inline std::vector<VkImageView>& getHeightmaps() { return heightmap_image_views_; }

关于c++ - 两个具有相同代码的代码库,但一个正在生成重载解析编译错误,而另一个则没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58023398/

相关文章:

c# - RichTextBox 从文本中删除转义字符

c++ - 使用免注册 COM 时 Powershell 无法使用 COM 对象

c++ - 同时使用 const 和 non-const 进行参数传递

java - GIN 中的 GFTP 绑定(bind)枚举

compiler-errors - 将选项传递给链接器时出现 "recompile with -fPIC"错误

node.js - 创建 react 应用程序+ Visual Studio + F5 = "Unexpected token import"

c++ - Qt C++ QTouchEvent & TouchPoint 混淆

c++ - 在 C++ 函数中使用内联优化有哪些注意事项?

c++ - 错误 C2146 : syntax error : missing ',' before identifier mType when passing a map by function

c# - 在 VS 2017 中更改整个项目的命名空间