Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Wierd Error

Wierd Error

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
5 Posts 3 Posters 344 Views
  • 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.
  • Y Offline
    Y Offline
    Yousef Alaa Hussain
    wrote on last edited by
    #1

    I am getting these Errors:
    Screenshot from 2024-08-24 18-25-47.png
    Whenever I Define a Variable in the "libraries.h" header file

    bool TextEditChangesSaved;
    

    This line is causing these errors, but when I make it a comment (or delete it)
    like here:
    Screenshot from 2024-08-25 18-30-17.png
    it works fine, and the program runs successfully, why am I getting this error and how can I fix it.
    Note: that these libraries.h file is included in all the files, and whenever I make a variable there, I get these errors.
    Thank you very much!

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      I don't see what's weird here - it's a basic c thing. You define a variable in an header so it's created in every compilation unit the header is included. You must only declare it in the header and define it once in the source (one definition rule) --> see e.g. https://www.geeksforgeeks.org/understanding-extern-keyword-in-c/

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      Y 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        I don't see what's weird here - it's a basic c thing. You define a variable in an header so it's created in every compilation unit the header is included. You must only declare it in the header and define it once in the source (one definition rule) --> see e.g. https://www.geeksforgeeks.org/understanding-extern-keyword-in-c/

        Y Offline
        Y Offline
        Yousef Alaa Hussain
        wrote on last edited by
        #3

        @Christian-Ehrlicher can you please tell me, in my case, how will the declaration be? Like what should I write in this header file and what should I write in other places, I need to use this variable in

        JonBJ 1 Reply Last reply
        0
        • Y Yousef Alaa Hussain

          @Christian-Ehrlicher can you please tell me, in my case, how will the declaration be? Like what should I write in this header file and what should I write in other places, I need to use this variable in

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Yousef-Alaa-Hussain
          What do you want to know that is not covered in the link @Christian-Ehrlicher gave you? How else do you want it expressed? In a C/C++ program you can have one occurrence (in some .c/.cpp file, top-level) of:

          int global_variable;
          

          In every other file which wants to access it you need

          extern int global_variable;
          

          Which you might put in a common .h file if you want it accessible in several other files.

          Y 1 Reply Last reply
          1
          • JonBJ JonB

            @Yousef-Alaa-Hussain
            What do you want to know that is not covered in the link @Christian-Ehrlicher gave you? How else do you want it expressed? In a C/C++ program you can have one occurrence (in some .c/.cpp file, top-level) of:

            int global_variable;
            

            In every other file which wants to access it you need

            extern int global_variable;
            

            Which you might put in a common .h file if you want it accessible in several other files.

            Y Offline
            Y Offline
            Yousef Alaa Hussain
            wrote on last edited by
            #5

            @JonB ok, thanks.
            I didn't understand where should I write the global variable, and didn't understand where should I write the extern word,
            but now I can understand so much! Thank you! And for @Christian-Ehrlicher

            1 Reply Last reply
            0
            • Y Yousef Alaa Hussain has marked this topic as solved on

            • Login

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