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. multiple definition of variable
QtWS25 Last Chance

multiple definition of variable

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 5.5k 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.
  • H Offline
    H Offline
    harry
    wrote on 11 Jan 2016, 18:48 last edited by
    #1

    hi.

    my compiler has reently started giving me "multiple definition of variable" type errors, and i dont understand why.

    as i understand it, defining variables in the top of the .cpp file makes them available to the whole class but not to other classes, correct? then it shouldn't matter that another class has variables with the same names, right? so why does the complier complain about 2 variables with the same name but in different classes?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 11 Jan 2016, 19:12 last edited by mrjj 1 Nov 2016, 19:14
      #2

      hi
      You should define variable inside the class. (in .h)
      that way it's available to whole class and not outside.

      If you define a global variable in cpp and this is used in mutiple places , (via the .o file)
      you might get multiple definitions.

      Short story is that you do not want global variables at all.

      class Myclass {
      private:
      int b; /// here should variables live
      float v;
      };

      H 1 Reply Last reply 12 Jan 2016, 15:06
      3
      • M mrjj
        11 Jan 2016, 19:12

        hi
        You should define variable inside the class. (in .h)
        that way it's available to whole class and not outside.

        If you define a global variable in cpp and this is used in mutiple places , (via the .o file)
        you might get multiple definitions.

        Short story is that you do not want global variables at all.

        class Myclass {
        private:
        int b; /// here should variables live
        float v;
        };

        H Offline
        H Offline
        harry
        wrote on 12 Jan 2016, 15:06 last edited by
        #3

        @mrjj said:

        hi
        You should define variable inside the class. (in .h)
        that way it's available to whole class and not outside.

        If you define a global variable in cpp and this is used in mutiple places , (via the .o file)
        you might get multiple definitions.

        Short story is that you do not want global variables at all.

        class Myclass {
        private:
        int b; /// here should variables live
        float v;
        };

        IOW, those variable definitions should be in the header, right? i put those definitions in the header, and the error messages disappeared. but then i added some more variables, and i started getting variable "was not declared in this scope" errors.

        and what exactly does "global" mean in this context? i need some variables that must be accessible to all methods of that class, but not to methods of other classes.

        V 1 Reply Last reply 12 Jan 2016, 15:31
        1
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 12 Jan 2016, 15:09 last edited by mrjj 1 Dec 2016, 15:10
          #4

          Hi
          Global in this context means, not in the class.
          Just declare them in the class, in the .h file and all functions
          of the class can use them.

          class MyPerson {
          int Age; // all in class can use
          QString Name; // all in class can use
          void Print(); can use all variables
          }
          (in .cpp)
          MyPerson::Print() {
          out << Name;
          }

          H 1 Reply Last reply 12 Jan 2016, 15:18
          0
          • M mrjj
            12 Jan 2016, 15:09

            Hi
            Global in this context means, not in the class.
            Just declare them in the class, in the .h file and all functions
            of the class can use them.

            class MyPerson {
            int Age; // all in class can use
            QString Name; // all in class can use
            void Print(); can use all variables
            }
            (in .cpp)
            MyPerson::Print() {
            out << Name;
            }

            H Offline
            H Offline
            harry
            wrote on 12 Jan 2016, 15:18 last edited by mrjj 1 Dec 2016, 15:23
            #5

            @mrjj
            -is private or public? or should i not even bother with public and private?

            If print is private then no other class can call it.
            same goes with variables. if private variables, then only class´s function can use them.

            So yes, care a lot about public and private.
            Public is for ok to other classes to know about.
            Private is for yourself.

            So the less other classes know about each other,
            the more fun it is to change something as not all classes need to be changed.

            So keep as much as private to class as possible.

            -so why do i get error messages? is something wrong with my Qt Creator?
            no, maybe u did something else ?
            did u put it Inside the class?

            H 1 Reply Last reply 12 Jan 2016, 15:34
            1
            • H harry
              12 Jan 2016, 15:06

              @mrjj said:

              hi
              You should define variable inside the class. (in .h)
              that way it's available to whole class and not outside.

              If you define a global variable in cpp and this is used in mutiple places , (via the .o file)
              you might get multiple definitions.

              Short story is that you do not want global variables at all.

              class Myclass {
              private:
              int b; /// here should variables live
              float v;
              };

              IOW, those variable definitions should be in the header, right? i put those definitions in the header, and the error messages disappeared. but then i added some more variables, and i started getting variable "was not declared in this scope" errors.

              and what exactly does "global" mean in this context? i need some variables that must be accessible to all methods of that class, but not to methods of other classes.

              V Offline
              V Offline
              ValentinMichelet
              wrote on 12 Jan 2016, 15:31 last edited by ValentinMichelet 1 Dec 2016, 15:33
              #6

              @harry
              There is nothing wrong with your Qt Creator. It uses a compiler that creates binary from your code, and you have some rules to know.

              Can I ask you how familiar with Oriented Object Programming (OOP) are you? And more specifically, with C++?

              If you don't have the main concepts of OOP, then you should get some tutorials from the Internet:
              Basic C++ (and OOP) concepts: http://www.tutorialspoint.com/cplusplus/cpp_object_oriented.htm
              What Wikipedia says about OOP: https://en.wikipedia.org/wiki/Object-oriented_programming

              Basically, in a class, you have access to everything, regardless of encapsulation level (public, protected or private).
              From outside, other classes have access to public declarations (member variables/methods).
              Finally, other classes have access to protected declarations if and only if they inherited from that class.

              1 Reply Last reply
              1
              • H harry
                12 Jan 2016, 15:18

                @mrjj
                -is private or public? or should i not even bother with public and private?

                If print is private then no other class can call it.
                same goes with variables. if private variables, then only class´s function can use them.

                So yes, care a lot about public and private.
                Public is for ok to other classes to know about.
                Private is for yourself.

                So the less other classes know about each other,
                the more fun it is to change something as not all classes need to be changed.

                So keep as much as private to class as possible.

                -so why do i get error messages? is something wrong with my Qt Creator?
                no, maybe u did something else ?
                did u put it Inside the class?

                H Offline
                H Offline
                harry
                wrote on 12 Jan 2016, 15:34 last edited by
                #7

                -so why do i get error messages? is something wrong with my Qt Creator?
                no, maybe u did something else ?
                did u put it Inside the class?

                the new variable definition is in the header file, just after Q_OBJECT. the other variables are just after the new one.

                button.h:
                class Button : public QWidget {
                Q_OBJECT
                QPointF VReference; // <-this one is the problem
                QString title;
                QColor buttonColor;
                int colorIndex;

                i don't understand why the other variable definitions work but this one doesn't.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 12 Jan 2016, 15:34 last edited by mrjj 1 Dec 2016, 15:35
                  #8

                  @harry said:

                  QPointF

                  did u include the header for it ?
                  #include <QPointF>

                  H 1 Reply Last reply 12 Jan 2016, 15:37
                  0
                  • M mrjj
                    12 Jan 2016, 15:34

                    @harry said:

                    QPointF

                    did u include the header for it ?
                    #include <QPointF>

                    H Offline
                    H Offline
                    harry
                    wrote on 12 Jan 2016, 15:37 last edited by
                    #9

                    @mrjj said:

                    did u include the header for it ?

                    while i was typing my last post, it occurred to me that i should check. there was no #include <QPointF> so i added it. but that didn't do me any good, i still get the same error.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 12 Jan 2016, 15:42 last edited by
                      #10

                      can you post the error?

                      H 1 Reply Last reply 12 Jan 2016, 15:46
                      0
                      • M mrjj
                        12 Jan 2016, 15:42

                        can you post the error?

                        H Offline
                        H Offline
                        harry
                        wrote on 12 Jan 2016, 15:46 last edited by
                        #11

                        @mrjj
                        this is the function:

                        QPointF VRef() {
                        return VReference;
                        }

                        and it gives the following error:

                        /home/harry/Projects/Qt/Desktop/Test/button.cpp:160: error: 'VReference' was not declared in this scope
                        return VReference;
                        ^

                        BTW, the ^ is supposed to be under the V in VReference.

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 12 Jan 2016, 15:47 last edited by mrjj 1 Dec 2016, 15:50
                          #12

                          hi
                          you must tell it it lives in Button
                          QPointF Button::VRef() {
                          return VReference;
                          }

                          and it should be listed in .h also
                          class Button : public QWidget {
                          Q_OBJECT
                          QPointF VReference; // <-this one is the problem
                          ...
                          QPointF VRef();

                          H 1 Reply Last reply 12 Jan 2016, 16:07
                          0
                          • M mrjj
                            12 Jan 2016, 15:47

                            hi
                            you must tell it it lives in Button
                            QPointF Button::VRef() {
                            return VReference;
                            }

                            and it should be listed in .h also
                            class Button : public QWidget {
                            Q_OBJECT
                            QPointF VReference; // <-this one is the problem
                            ...
                            QPointF VRef();

                            H Offline
                            H Offline
                            harry
                            wrote on 12 Jan 2016, 16:07 last edited by
                            #13

                            @mrjj said:

                            hi
                            you must tell it it lives in Button
                            QPointF Button::VRef() {

                            i guess i should have known. i corrected it and now it works. thanx.

                            1 Reply Last reply
                            0

                            9/13

                            12 Jan 2016, 15:37

                            • Login

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