错误:即使指针设置为对象,为什么“void*"也不是指向对象的指针类型? [英] Error: Why 'void*' is not a pointer-to-object type even though the pointer is set to an object?

查看:30
本文介绍了错误:即使指针设置为对象,为什么“void*"也不是指向对象的指针类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码(在 Coliru 上直播):

// untouchable extern library .hpp  file

typedef union ExternLibraryUnion
{
    int a;
    float b;
}ExternLibraryUnion;

// my code

#include <iostream>

class Container{
    public:
    Container() : m_union(NULL) {};

    ~Container(){
        if(m_union){
            delete m_union;
        }
    }

    void init(){
        m_union = new ExternLibraryUnion();
    }

    ExternLibraryUnion* get_union(){
        return m_union;
    }

private:
    ExternLibraryUnion* m_union;
};


class Master{
    public:
    Master() : m_union(NULL) {
        m_container.init();    
    };

    ~Master(){
        if(m_union){
            delete static_cast<ExternLibraryUnion*>(m_union);
        }
    }

    void load(){

    }

    void set(int i){
        m_union = m_container.get_union();
        m_union->a = i;
    }

    void* get_union(){
        return m_union;
    }

private:
    void* m_union;
    Container m_container;
};

class Worker{
    public:
    Worker() : m_extern_library_union(NULL) {};

    ~Worker(){
        if (m_extern_library_union){
            delete m_extern_library_union;
        }
    }

    void load(Master& master){
        m_extern_library_union = reinterpret_cast<ExternLibraryUnion*>(master.get_union());
    }

    int get_int(){
        return m_extern_library_union->a;
    }


private:
    ExternLibraryUnion* m_extern_library_union;
};

int main()
{
    Master master;
    master.set(3);

    Worker worker;
    worker.load(master);

    std::cout << worker.get_int() << std::endl;
}

代码产生:

main.cpp: In member function 'void Master::set(int)':
main.cpp:55:16: error: 'void*' is not a pointer-to-object type
         m_union->a = i;
                ^~

在外部库中,定义了一个 union ExternLibraryUnion,我在自己的代码中使用了它.我无法理解的问题是在 Master 类的 set 方法中.Master 成员 void* m_union 应该指向存储在成员 Container m_container 中的联合.当我设置 m_union = m_container.get_union() 时,编译器应该能够知道我从 get_union() 方法调用.所以我不太明白分配 m_union->a = i 引起的错误.当然,void* 没有类型,但我为它分配了一个精确类型 ExternLibraryUnion 的指针.

In an extern library, a union ExternLibraryUnion is defined which I'm using inside my own code. My problem, which I can't get my head around, is in the set method of class Master. The Master member void* m_union should point to the union stored inside the member Container m_container. As I'm setting the m_union = m_container.get_union() the compiler should be able to know that I'm getting a ExternLibraryUnion* back from the get_union() method call. So I don't quite the error arising from the assignment m_union->a = i. Sure, a void* has no type, but I assigned it a pointer of the precise type ExternLibraryUnion.

假设我不能直接接触 Container m_container 对象.我需要通过 void* m_union 指针进行分配.

Let's also say I can not touch the Container m_container object directly. I need to make the assigned through the void* m_union pointer.

非常感谢任何帮助!

推荐答案

编译器不知道m_union可能实际上指向什么.您将其声明为 void * 以便编译器相信您,它别无选择.这就是它知道的全部,所以 m_union->a 必须被标记为错误,因为 ->a 没有意义编译器在这里.

The compiler has no clue what m_union might actually be pointing at. You declared it as a void * so the compiler believes you, it has no choice. And that's all it knows, so m_union->a has to be flagged as an error, because ->a has no meaning to the compiler here.

换句话说,将 RTTI 放在一边,指针不知道"它们指向的是什么.编译器只知道指针是如何声明的.

To put it another way, RTTI aside, pointers don't 'know' what they're pointing at. The compiler only knows how the pointer was declared.

我不知道还能说什么,就这么简单.我不喜欢这样说,但是从整体上看代码,它看起来像一团糟.谁写的?

I don't know what else to say, it's really that simple. I don't like having to say this, but looking at the code as a whole, it looks like a complete mess. Who wrote it?

Jeffrey 所说的确实可以解决问题,但这不是您要的.

And what Jeffrey said will indeed fix it, but that's not what you asked.

这篇关于错误:即使指针设置为对象,为什么“void*"也不是指向对象的指针类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆