Undefined reference to 'operator delete(void*)'

前端 未结 4 2028
长发绾君心
长发绾君心 2021-02-12 22:09

I\'m new to C++ programming, but have been working in C and Java for a long time. I\'m trying to do an interface-like hierarchy in some serial protocol I\'m working on, and kee

4条回答
  •  余生分开走
    2021-02-12 22:47

    If you're just looking to do an interface, you don't need new/delete. Just remove the "virtual" from the base class destructor, and make sure the derived class has an implementation of __cxa_pure_virtual().

    Here is a compilable example. (I removed the returns to keep things simple, but it works just fine with them.)

    In PacketWriter.h

    class PacketWriter {
    public:
        virtual void nextByte() = 0;
    protected:
        ~PacketWriter() {}
    };
    

    In StringWriter.h

    #include "PacketWriter.h"
    
    class StringWriter : public PacketWriter {
    public:
        StringWriter(const char* message);
        void nextByte();
    };  
    

    In StringWriter.cpp

    #include "StringWriter.h"
    
    // Definition of the error function to call if the constructor goes bonkers
    extern "C" void __cxa_pure_virtual() { while (1); }
    
    StringWriter::StringWriter(const char* message)
    {
        // constructor code here
    }
    
    void StringWriter::nextByte()
    {
    }
    

    Compile with avr-g++ StringWriter.cpp

提交回复
热议问题