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
Forum Updated to NodeBB v4.3 + New Features

multiple definition of variable

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 5.7k Views 3 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #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
    3
    • mrjjM mrjj

      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 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.

      ValentinMicheletV 1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #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
        0
        • mrjjM mrjj

          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 last edited by mrjj
          #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
          1
          • H harry

            @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.

            ValentinMicheletV Offline
            ValentinMicheletV Offline
            ValentinMichelet
            wrote on last edited by ValentinMichelet
            #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

              @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 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
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #8

                @harry said:

                QPointF

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

                H 1 Reply Last reply
                0
                • mrjjM mrjj

                  @harry said:

                  QPointF

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

                  H Offline
                  H Offline
                  harry
                  wrote on 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
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    can you post the error?

                    H 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      can you post the error?

                      H Offline
                      H Offline
                      harry
                      wrote on 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
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #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
                        0
                        • mrjjM mrjj

                          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 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

                          • Login

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