无法计算调用中 C++ 函数指针类型和实际 C++ 函数类型之间的类型差异

问题描述 投票:0回答:1
template < class DATA_T > 
class QuickSort {
    public:
        typedef int (*tComparator) (const DATA_T*, const DATA_T*);
        void SortAscending(DATA_T* buffer, size_t left, size_t right, tComparator compare);
};

typedef struct {
    const char* name;
    const char* vs;
    const char* fs;
    Shader* shader;
} tShaderData;


class ShaderCode : public Shader {
public:
    Array<tShaderData*>* m_shaders;

    void CreateShaders(void);

    ShaderCode()
        : m_shaders(nullptr)
    { }

    static int CompareShaders(const tShaderData** s1, const tShaderData** s2);
};

int ShaderCode::CompareShaders(const tShaderData** s1, const tShaderData** s2) {
    return strcmp((*s1)->name, (*s2)->name);
}

void ShaderCode::CreateShaders(void) {
    m_shaders = new Array<tShaderData*> ({
        &colorShader,
        &textureShader,
        &outlineShader,
        &boxBlurShader,
        &fxaaShader,
        &gaussBlurShader,
        &fireworks1Shader,
        &fireworks2Shader,
        &fireworks3Shader,
        &fireworks4Shader
        });
    QuickSort<tShaderData*> qs;
    tShaderData** dataPtr = m_shaders->Data();
    qs.SortAscending(dataPtr, 0, m_shaders->Capacity() - 1, ShaderCode::CompareShaders);
    //m_shaders->SortAscending((QuickSort<tShaderData*>::tComparator) ShaderCode::CompareShaders);
}

Array
是一个自行实现的数组管理类,类似于
std::vector

错误信息:

QuickSort::SortAscending:无法转换所有参数类型。

使用 VS 2022、C++ 20、调试模式编译。所有功能都是

__decl
。我无法弄清楚这里类型的差异。

c++ function-pointers
1个回答
0
投票

对于 C++,我希望有更多类似这样的东西:

#include <string>
#include <algorithm>
#include <vector>

// C++ style struct declaration
struct ShaderData
{
    std::string name;
    std::string vertex_shader;      // give your variables meaningfull names (I am guessing here)
    std::string fragment_shader; 
};

class ShaderCode
{
public:
    //Shader code will take over ownership of your ShaderData
    ShaderCode(std::vector<ShaderData>&& shader_data) : 
        m_shader_data{std::move(shader_data)}
    {
    }

    void sort()
    {
        std::ranges::sort(m_shader_data, {}, &ShaderData::name );
    }

private:
    std::vector<ShaderData> m_shader_data;
};

int main()
{
    ShaderCode{{
        {"colorShader", "...", "..."},
        {"textureShader", "...", "..."},
        {"outlineShader", "...", "..."},
        {"fireworks1Shader", "...", "..."},
        {"fireworks2Shader", "...", "..."}
    }};
}
© www.soinside.com 2019 - 2024. All rights reserved.