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. [SOLVED] #include does not behave as i thought i
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] #include does not behave as i thought i

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 1.5k Views 2 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.
  • B Offline
    B Offline
    bvox
    wrote on last edited by bvox
    #1

    Good Evening.
    First steps in Linux C ++ and I'm trying to convert to C ++ an old program written in Visual Basic (version for Visual Studio 10).
    Use QtCreator and Qt, but I think the problem is not of Qt, but some particular C ++ in general do not know.
    If the first part of the main write so:

        #include <QtWidgets>
    
        QMainWindow  *X_mainWindow;
        QMenuBar     *X_menuBar;
        QWidget     *X_centralWidget;
    
        int main(int argc, char *argv[])
    

    and then there is a .h file in which the same variables I declared "extern", the program works correctly.

    As if those same three variables I write to a file "dichiarative_unatantum.h" and I include in the main well:

        #include <QtWidgets>
    
        #include "dichiarative_unatantum.h"
    
        int main(int argc, char *argv[])
    

    in compilation gives me errors like "multiple definition of X_mainWindow".
    Why is this happening? The #include directive should not simulate the insertion at that particular point of the three global variables?
    Give me a hand to understand? Thank you.

    B 1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      is the file dichiarative_unatantum.h included only once in your program?
      Can you show both that file and the one where the variables are declared as extern??

      TIP: try to avoid to use global/extern variables; they make the code hard to mantain and debug.

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • B bvox

        Good Evening.
        First steps in Linux C ++ and I'm trying to convert to C ++ an old program written in Visual Basic (version for Visual Studio 10).
        Use QtCreator and Qt, but I think the problem is not of Qt, but some particular C ++ in general do not know.
        If the first part of the main write so:

            #include <QtWidgets>
        
            QMainWindow  *X_mainWindow;
            QMenuBar     *X_menuBar;
            QWidget     *X_centralWidget;
        
            int main(int argc, char *argv[])
        

        and then there is a .h file in which the same variables I declared "extern", the program works correctly.

        As if those same three variables I write to a file "dichiarative_unatantum.h" and I include in the main well:

            #include <QtWidgets>
        
            #include "dichiarative_unatantum.h"
        
            int main(int argc, char *argv[])
        

        in compilation gives me errors like "multiple definition of X_mainWindow".
        Why is this happening? The #include directive should not simulate the insertion at that particular point of the three global variables?
        Give me a hand to understand? Thank you.

        B Offline
        B Offline
        bvox
        wrote on last edited by
        #3

        First of all sorry for my english: I translate from Italian with google translator.
        In the version that does not give error do:

        // main.cpp
        #include "w000_dichiarative.h"
        #include "w001_main.h"
        #include "w010_partita1.h"
        
        #include <QtWidgets>
        
        // #include "w000_dichiarative_unatantum.h" invalidated
        
        QMainWindow  *X_mainWindow;
        QMenuBar     *X_menuBar;
        QWidget     *X_centralWidget;
        
        int main(int argc, char *argv[])
        {
        

        The first of "w000_dichiarative.h" is so:

        // W000_dichiarative.h
        #ifndef W000_DICHIARATIVE_H
        #define W000_DICHIARATIVE_H
        
        #include <QtWidgets>
        
        extern QMainWindow  *X_mainWindow;
        extern QMenuBar     *X_menuBar;
        extern QWidget     *X_centralWidget;
        

        So it works properly.

        But if I substitute the main way:

        #include "w000_dichiarative.h"
        #include "w001_main.h"
        #include "w010_partita1.h"
        
        #include <QtWidgets>
        
        #include "w000_dichiarative_unatantum.h"
        
        // QMainWindow  *X_mainWindow; invalidated
        // QMenuBar     *X_menuBar; invalidated
        // QWidget     *X_centralWidget; invalidated
        
        int main(int argc, char *argv[])
        {
        

        and son-in "w000_dichiarative_unatantum.h" thus:

        #ifndef W_DICHIARATIVEUNATANTUM_H
        #define W_DICHIARATIVEUNATANTUM_H
        
        #include <QtWidgets>
        
         QMainWindow  *X_mainWindow;
         QMenuBar     *X_menuBar;
         QWidget     *X_centralWidget;
        

        compilation gives me the error mentioned before. Why?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          Hi,

          no problem for the English (I'm Italian too and you can also use the Italia forum to write in your native language).

          Can you post the code? is that header file included only once?

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bvox
            wrote on last edited by
            #5

            Ok Thanks.
            This morning I had asked for help in the forum Linux Mint-Italian, but I had no answer. 2 days ago I wrote two questions on the forum Qt-Italian but nobody deigned to answer me. This is why I wrote in the American forum: you are more prepared and kind.
            The file unatantum.h is inserted only in the main and only in the version that gives error. In the version that works well is not included even in the .pro

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bvox
              wrote on last edited by
              #6

              Oh nooo, I had left a #include "... a tantum.h" in another file. I deleted and now it works. But the initial #ifndef would not have to avoid duplication of definitions? In any case, thank you, thank you very much.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mcosta
                wrote on last edited by
                #7

                Hi,

                I mean this forum.

                BTW, the include guard

                #ifndef XXXX
                #define XXXX
                ...
                #endif
                

                avoids to include twice the same header in a source file but doesn't avoid that you include the same header in two different source file.

                Once your problem is solved don't forget to:

                • Mark the thread as SOLVED using the Topic Tool menu
                • Vote up the answer(s) that helped you to solve the issue

                You can embed images using (http://imgur.com/) or (http://postimage.org/)

                1 Reply Last reply
                0

                • Login

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