简体   繁体   English

未定义对“ typeinfo”的引用 <class> ”

[英]Undefined Reference to “typeinfo for <class>”

I saw questions about this problem but i can't find any solutions working for me. 我看到了有关此问题的问题,但找不到适合我的解决方案。 I am using Visual Code with C++ and C++ version is anterior to C++ 11. I have DNS classes, with various classes inheriting a principal DNS message class, this way I can factorize some attributes. 我将Visual Code与C ++一起使用,并且C ++版本早于C ++11。我有DNS类,各种类继承了主要的DNS消息类,这样我就可以分解某些属性。 In function of the type type field I can know the type of the object. 在类型类型字段的功能中,我可以知道对象的类型。 When I compile I have an error "undefined reference to "typeinfo for DNS_RR_A" for example, but I got this at each dynamic cast I am doing to check the class of the object. 例如,当我编译时,出现错误“对“针对DNS_RR_A的typeinfo的未定义引用””,但是我在每次动态转换时都得到了此信息,以检查对象的类。

My dnsMessage.cpp only have constructor and destructor. 我的dnsMessage.cpp只有构造函数和析构函数。

here is my classes: 这是我的课程:

class CDnsMessage
{       
    public:
        CDnsMessage();
        virtual ~CDnsMessage();

        virtual void GetSize() = 0;

        uint32_t       m_ttl;
        eDnsClass      m_class;
        eDnsType       m_type; 
        std::string    m_domain; 
        uint8_t        m_sizeDnsCorpse;
        uint8_t        m_sizeDomainName; 
};

class CDns_RR_A : public CDnsMessage
{
    public:
        CDns_RR_A();
        virtual ~CDns_RR_A();

        virtual void GetSize() {/*....*/}

        uint32_t        m_address;
};

And here is a sample of my function using with the error at the dynamic cast. 这是我的函数示例,在动态转换时使用了错误。 I receive a message I have to encode but I don't know the nature of the message so I dynamic cast so I can adapt my encode: 我收到了我必须编码的消息,但我不知道消息的性质,因此我进行了动态投射,因此可以调整编码:

//i receive a message i have to encode, i don't know the type 
void EncodeOpaqueData(CDnsMessage & msg, std::vector<uint8_t>& output)
{
//where i encode
    output.clear();

    // Error : "undefined reference to `typeinfo for CDns_RR_A'"
    if(dynamic_cast< CDns_RR_A* >( &msg ) != NULL) 
    {       
            CDns_RR_A* RR_A_msg = dynamic_cast< CDns_RR_A* >( &msg );

            uint16_t dnstype = cmn_hton16(1);
            output.push_back(dnstype);
            output.push_back(dnstype >> 8);
            /* stuff here */

            uint32_t address = cmn_hton32(RR_A_msg->m_address);
            for (int i = 0; i < 4; i++)
            {
                    output.push_back(static_cast<uint8_t>(address >>(i * 8)));
            }

    }
}

After think more of the function, instead of checking the type of the object I could check msg->m_type and adapt in function of the type, the m_type variable can be wrongly instantiated for example. 在考虑了更多的功能之后,除了检查对象的类型,我可以检查msg-> m_type并适应类型的函数,例如,可以错误地实例化m_type变量。 But anyway I would like to understand this error and how to fix it. 但是无论如何,我想了解这个错误以及如何解决。 thanks in advance. 提前致谢。

Try to change the reference parameter to a pointer like this: 尝试将参照参数更改为这样一个指针:

void EncodeOpaqueData(CDnsMessage * msg, std::vector<uint8_t>& output)

You have the explanation here: 您在这里有解释:

Difference in behavior while using dynamic_cast with reference and pointers 将dynamic_cast与引用和指针一起使用时行为上的差异

The class impedimenta—virtual method table and typeinfo—are generated when the first declared virtual method is compiled. 当编译第一个声明的虚拟方法时,将生成impedimenta类(虚拟方法表和typeinfo)。 Are you defining the virtual ~CDnsMessage(); 您是否正在定义 virtual ~CDnsMessage(); (ie CDnsMessage::~CDnsMessage() {} ) and is the file where it is defined included in the link. (即CDnsMessage::~CDnsMessage() {} ),并且是链接中包含该文件的定义文件。

Note that out-of-line definitions are not weak, so it must be defined in exactly one source (not header) file. 请注意,离线定义并不弱,因此必须在一个源文件(而不是标头文件)中定义它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未定义对typeinfo的代码综合生成类的引用 - Undefined reference to typeinfo for Code Synthesis generated Class 对`typeinfo和&#39;vtable的未定义引用 - undefined reference to `typeinfo and 'vtable 对C ++编译虚拟类的`typeinfo的未定义引用 - Undefined reference to `typeinfo for ' c++ compile virtual class g++ 对 typeinfo 的未定义引用 - g++ undefined reference to typeinfo OMNeT ++:未定义对inet :: IMobility的typeinfo的引用 - OMNeT++: undefined reference to `typeinfo for inet::IMobility' 与nsRunnable链接时对typeinfo的未定义引用 - Undefined reference to typeinfo when linking with nsRunnable 错误:未定义对 &#39;typeinfo for std::exception&#39; 的引用 - error: undefined reference to 'typeinfo for std::exception' 错误:未定义对静脉的类型信息的引用::BatteryAccess - Error: undefined reference to `typeinfo for veins::BatteryAccess' C ++:除了虚拟函数以外,“未定义引用&#39;typeinfo for [class name]&#39;”的原因是什么 - C++: what are the causes of “ undefined reference to 'typeinfo for [class name]' ”other than virtual functions 为什么-fsanitize = undefined导致“对typeinfo的未定义引用”? - Why does -fsanitize=undefined cause “undefined reference to typeinfo”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM