Multiple definition WTF



  • I created 2 functions for convertions. I put them into the globally avaliable fheader file. Which has protection of multiple includes. But I get 47 errors D:\dev\app\DataTypes\enums.h:18: ошибка: multiple definition of (QByteArray)' (24 for each function). The problems are found in MOC files D:\dev\build-app-Desktop_Qt_5_7_0_MinGW_32bit-Debug\debug\moc_usergroupscontroller.o:-1: In functionZN12QMapNodeBase25callDestructorIfNecessaryIyEEN9QtPrivate9QEnableIfIXntsr9QTypeInfoIT_E9isComplexEvE4TypeERS4_':`
    What is wrong??

    /// converts quintptr to QByteArray object
    QByteArray convert(quintptr t_arg) {
        QByteArray converted;
        QDataStream stream(&converted, QIODevice::WriteOnly);
        stream << t_arg;
        return converted;
    }
    /// converts QByteArray to quintptr object
    quintptr convert(QByteArray t_arg) {
        //QByteArray AvailableId = getlastMaxid();
        quintptr converted;
        QDataStream stream(&t_arg, QIODevice::ReadOnly);
        stream >> converted;
        return converted;
    }
    


  • @Kofr said in Multiple definition WTF:

    I put them into the globally avaliable fheader file.

    You should put function bodies in .cpp file. Header file must hold only forward declarations without function body:

    QByteArray convert(quintptr t_arg);
    quintptr convert(QByteArray t_arg);
    

  • Moderators

    @Kofr

    The protection of multiple includes does work only for recursive inclusion.
    E.g. main.cpp includes header1.h includes header2.h includes header1.h
    The last include will not happen.

    You probably have
    main.cpp includes header1.h includes header2.h
    inpHeader1.cpp includes header1.h

    There it will be includes twice.

    You can do

    /// converts quintptr to QByteArray object
    inline QByteArray convert(quintptr t_arg) {
        QByteArray converted;
        QDataStream stream(&converted, QIODevice::WriteOnly);
        stream << t_arg;
        return converted;
    }
    /// converts QByteArray to quintptr object
    inline quintptr convert(QByteArray t_arg) {
        //QByteArray AvailableId = getlastMaxid();
        quintptr converted;
        QDataStream stream(&t_arg, QIODevice::ReadOnly);
        stream >> converted;
        return converted;
    }
    

    There the functions will be generated where required. It is matter of taste if you really like to do it this way or rather setup a cpp-file and do it the standard way.



  • This post is deleted!

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.