Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt 4.8, CPP : mutiple definiation
Qt 6.11 is out! See what's new in the release blog

Qt 4.8, CPP : mutiple definiation

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anas_Deshmukh
    wrote on last edited by Anas_Deshmukh
    #1

    hi everyone,

    I am having multiple defination err in my qt desktop application (qt 4.8.6 + ubuntu 12.04).

    below is my demo code.

    // global.h
    #ifndef GLOBAL_H
    #define GLOBAL_H
    
    #include <QtCore>
    #include <QObject>
    #include <QDebug>
    QString temp;
    
    #endif // GLOBAL_H
    
    
    // class1.h
    
    #ifndef CLASS1_H
    #define CLASS1_H
    
    #include <QtCore>
    #include <QObject>
    #include <QDebug>
    
    class class1: public QObject
    {
        Q_OBJECT
    public :
        Q_INVOKABLE QString class1_foo();
    };
    
    #endif // CLASS1_H
    

    // class2.h

    #ifndef CLASS2_H
    #define CLASS2_H
    
    #include <QtCore>
    #include <QObject>
    #include <QDebug>
    
    
    class class2: public QObject
    {
        Q_OBJECT
    public :
        Q_INVOKABLE QString class2_foo();
    };
    
    #endif // CLASS2_H
    

    //class1.cpp

    #include "class1.h"
    #include "class2.h"
    
    #include "global.h"
    
    QString class1::class1_foo() {
    
        qDebug() << "this is class 1";
    
    
        return "this is class 1";
    }; 
    
    // class2.cpp
    
    #include "class1.h"
    #include "class2.h"
    
    #include "global.h"
    
    QString class2::class2_foo() {
    
        class1 class1_obj;
        class1_obj.class1_foo();
    
        qDebug() << "this is class 2";
        return "this is class 2";
    } 
    

    //errors :
    /home/user/anas/qtProject/build-multiple_def_err-Desktop-Debug/class2.o:-1: In function `QFlagsQIODevice::OpenModeFlag::QFlags(QIODevice::OpenModeFlag)':

    /home/user/anas/qtProject/multiple_def_err/class2.cpp:6: multiple definition of `temp'
    /home/user/anas/qtProject/multiple_def_err/class1.cpp:7: error: first defined here

    : error: collect2: error: ld returned 1 exit status

    Venkatesh VV 1 Reply Last reply
    0
    • Vinod KuntojiV Offline
      Vinod KuntojiV Offline
      Vinod Kuntoji
      wrote on last edited by
      #2

      @Anas_Deshmukh ,

      declare like this,
      const QString temp;

      C++, Qt, Qt Quick Developer,
      PthinkS, Bangalore

      1 Reply Last reply
      1
      • A Anas_Deshmukh

        hi everyone,

        I am having multiple defination err in my qt desktop application (qt 4.8.6 + ubuntu 12.04).

        below is my demo code.

        // global.h
        #ifndef GLOBAL_H
        #define GLOBAL_H
        
        #include <QtCore>
        #include <QObject>
        #include <QDebug>
        QString temp;
        
        #endif // GLOBAL_H
        
        
        // class1.h
        
        #ifndef CLASS1_H
        #define CLASS1_H
        
        #include <QtCore>
        #include <QObject>
        #include <QDebug>
        
        class class1: public QObject
        {
            Q_OBJECT
        public :
            Q_INVOKABLE QString class1_foo();
        };
        
        #endif // CLASS1_H
        

        // class2.h

        #ifndef CLASS2_H
        #define CLASS2_H
        
        #include <QtCore>
        #include <QObject>
        #include <QDebug>
        
        
        class class2: public QObject
        {
            Q_OBJECT
        public :
            Q_INVOKABLE QString class2_foo();
        };
        
        #endif // CLASS2_H
        

        //class1.cpp

        #include "class1.h"
        #include "class2.h"
        
        #include "global.h"
        
        QString class1::class1_foo() {
        
            qDebug() << "this is class 1";
        
        
            return "this is class 1";
        }; 
        
        // class2.cpp
        
        #include "class1.h"
        #include "class2.h"
        
        #include "global.h"
        
        QString class2::class2_foo() {
        
            class1 class1_obj;
            class1_obj.class1_foo();
        
            qDebug() << "this is class 2";
            return "this is class 2";
        } 
        

        //errors :
        /home/user/anas/qtProject/build-multiple_def_err-Desktop-Debug/class2.o:-1: In function `QFlagsQIODevice::OpenModeFlag::QFlags(QIODevice::OpenModeFlag)':

        /home/user/anas/qtProject/multiple_def_err/class2.cpp:6: multiple definition of `temp'
        /home/user/anas/qtProject/multiple_def_err/class1.cpp:7: error: first defined here

        : error: collect2: error: ld returned 1 exit status

        Venkatesh VV Offline
        Venkatesh VV Offline
        Venkatesh V
        wrote on last edited by
        #3

        @Anas_Deshmukh
        Hi,
        This error is because of adding include statement of class A in class B and class B in class A.
        in class A you dont requires class B statement. remove that one, it will works fine.

        1 Reply Last reply
        1
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          your linker is right. You can't have the same temp defined in 2 different object files.

          the solution is change QString temp; into extern QString temp; and then create a global.cpp file in which you put

          #include "global.h"
          QString temp;
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          4
          • A Offline
            A Offline
            Anas_Deshmukh
            wrote on last edited by
            #5

            @VRonin Thanak you. your solution work fine

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved