C ++:类指针迭代器指向的类中struct的迭代器不可解除引用

时间:2018-04-29 08:45:49

标签: c++ pointers iterator

在这段代码中,'类指针的向量迭代器'和'向量的向量迭代器'循环总共两次。

但是for (vector<VkQueueFamilyProperties>::iterator iter2...导致以下异常:

  

vector iterator not dereferencable

我是否以错误的方式访问迭代器?

我想知道为什么第二个迭代器无法初始化。

RVulkan.cpp的一部分:

#include "Core/AppInfo.h"
#include "RVulkan/RVulkan.h"

/*
Some source code for class function definition ...
*/

bool RVulkan::Initialize()
{
    VkResult result;
    result = CreateInstance();
    if (result != VK_SUCCESS) {
        throw runtime_error("Failed to create vulkan instance");
        return false;
    }

    EnumeratePhysicalDevice();
    bool found = false;
    for (vector<VkPhysicalDevice>::iterator iter
        = pDevice.begin(); iter != pDevice.end(); ++iter)
    {
        RVulkanDevice RVulkanDeviceClassTemp(this, &(*iter));
        RVulkanDeviceClass.emplace_back(move(&RVulkanDeviceClassTemp));
        found = true;
    }

    if (!found) {
        throw runtime_error("Failed to find any graphics devices with Vulkan supported");
        return false;
    }

    bool foundGraphic = false;
    bool foundCompute = false;
    for (vector<RVulkanDevice*>::iterator iter
        = RVulkanDeviceClass.begin(); iter != RVulkanDeviceClass.end(); ++iter)
    {
        (*iter)->GetPhysicalDeviceQueueFamilyProperties();

        /* exceptions: vector iterator not dereferencable */
        for (vector<VkQueueFamilyProperties>::iterator iter2
            = (*iter)->queueFamilyProps.begin(); iter2 != (*iter)->q   ueueFamilyProps.end(); ++iter2)
        {
            if (iter2->queueFlags & VK_QUEUE_GRAPHICS_BIT) {
                foundGraphic = true;
            }
            if (iter2->queueFlags & VK_QUEUE_COMPUTE_BIT) {
                foundCompute = true;
            }
        }
    }

    if (!foundGraphic && !foundCompute) {
        throw runtime_error("Failed to find any graphics devices for draw and compute with Vulkan supported");
        return false;
    }
    else if (!foundGraphic) {
        throw runtime_error("Failed to find any graphics devices for draw with Vulkan supported");
        return false;
    }
    else if (!foundCompute) {
        throw runtime_error("Failed to find any graphics devices for compute devices with Vulkan supported");
        return false;
    }

    return true;
}

RVulkan.h:

#pragma once

#include "RVulkan/RVulkanHeader.h"
#include "RVulkan/RVulkanUtil.h"
#include "RVulkan/RVulkanDevice.h"
#include "RVulkan/RVulkanCommandBuffer.h"

class RVulkanDevice;
class RVulkanCommandBuffer;

class RVulkan
{
public:
    RVulkan();
    RVulkan(const RVulkan& copy);
    RVulkan(const RVulkan&& copy);
    ~RVulkan();

    bool Initialize();
    void DeInitialize();
    void Update();

public:
    vector<const char *> instanceLayerNames;
    vector<const char *> instanceExtensionNames;
    vector<RVkLayerProperties> instanceLayerProperties;
    vector<VkExtensionProperties> instanceExtensionProperties;

    VkInstance instance;
    vector<VkPhysicalDevice> pDevice;
    vector<RVulkanDevice*> RVulkanDeviceClass;
    vector<RVulkanCommandBuffer*> RVulkanCommandBufferClass;

private:
    VkResult CreateInstance();
    void DestroyInstance();

    VkResult EnumeratePhysicalDevice();
};

RVulkanDevice.h:

#pragma once

#include "RVulkan/RVulkanHeader.h"
#include "RVulkan/RVulkanUtil.h"
#include "RVulkan/RVulkan.h"

class RVulkan;

class RVulkanDevice
{
    friend class RVulkan;
public:
    RVulkanDevice(RVulkan* _RVulkanClass, VkPhysicalDevice* _pDevice);
    ~RVulkanDevice();
    RVulkanDevice(const RVulkanDevice& copy);
    RVulkanDevice(const RVulkanDevice&& copy);
    RVulkanDevice& operator=(const RVulkanDevice& copy);
    RVulkanDevice& operator=(const RVulkanDevice&& copy);

protected:
    bool GetPhysicalDeviceQueueFamilyProperties();
    VkResult AddDevice(const uint32_t _queueFamilyIndex);
    bool RemoveDevice(const VkDevice _device);
    bool RemoveDevice(const uint32_t _deviceIndex);
    bool RemoveAllDevice();

public:
    RVulkan* RVulkanClass;
    VkPhysicalDevice* pDevice;

    vector<VkQueueFamilyProperties> queueFamilyProps;
    vector<VkDevice> device;
};

RVulkanDevice.cpp - GetPhysicalDeviceQueueFamilyProperties:

bool RVulkanDevice::GetPhysicalDeviceQueueFamilyProperties()
{
    uint32_t queueFamilyPropsCount;

    vkGetPhysicalDeviceQueueFamilyProperties(*pDevice, &queueFamilyPropsCount, NULL);
    if (queueFamilyPropsCount <= 0) {
        return false;
    }
    queueFamilyProps.resize(queueFamilyPropsCount);
    vkGetPhysicalDeviceQueueFamilyProperties(*pDevice, &queueFamilyPropsCount, queueFamilyProps.data());

    return true;
}

RVulkanHeader.h:

#pragma once

#include <vector>
#include <stdint.h>
#include <system_error>
#include <assert.h>

#include <vulkan/vulkan.h>

using namespace std;

Debugged with VS2017

Debugged with VS2017 2

Full Source Code (need vulkan)

0 个答案:

没有答案