multiple definition of variable



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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

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



  • @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?



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



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


  • Qt Champions 2016

    @harry said:

    QPointF

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



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


  • Qt Champions 2016

    can you post the error?



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


  • Qt Champions 2016

    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();



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.