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. Global QVector struct how to?
QtWS25 Last Chance

Global QVector struct how to?

Scheduled Pinned Locked Moved Solved General and Desktop
globalqvectorstruct
3 Posts 2 Posters 479 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.
  • Q Offline
    Q Offline
    qDebug
    wrote on 24 Oct 2022, 21:44 last edited by
    #1

    Hey guys!

    Need some help on how to get a global QVector and a struct working without LNK2001/LNK2005 errors.

    Here is my global.h file:

    #ifndef GLOBAL_H
    #define GLOBAL_H
    
    #include <QString>
    #include <QVector>
    
    struct gUser {
        int id;
        QString name;  
    };
    
    extern QVector<gUser> Users;
    
    #endif // GLOBAL_H
    

    In my cpp files i include the global.h from above and get LNK errors. I did try serval other ways but non of them works so far. Is there something wrong with my declaration or? I don't know and hope some onre here can help me out.

    Thanks!

    J 1 Reply Last reply 24 Oct 2022, 22:18
    0
    • Q qDebug
      24 Oct 2022, 21:44

      Hey guys!

      Need some help on how to get a global QVector and a struct working without LNK2001/LNK2005 errors.

      Here is my global.h file:

      #ifndef GLOBAL_H
      #define GLOBAL_H
      
      #include <QString>
      #include <QVector>
      
      struct gUser {
          int id;
          QString name;  
      };
      
      extern QVector<gUser> Users;
      
      #endif // GLOBAL_H
      

      In my cpp files i include the global.h from above and get LNK errors. I did try serval other ways but non of them works so far. Is there something wrong with my declaration or? I don't know and hope some onre here can help me out.

      Thanks!

      J Offline
      J Offline
      JonB
      wrote on 24 Oct 2022, 22:18 last edited by JonB
      #2

      @qDebug said in Global QVector struct how to?:

      extern QVector<gUser> Users;

      One (and only one) .cpp file must define storage for the variable:

      QVector<gUser> Users;
      

      The fact that it is a vector of structs is not relevant.

      1 Reply Last reply
      1
      • Q Offline
        Q Offline
        qDebug
        wrote on 25 Oct 2022, 05:16 last edited by
        #3

        Thank you, that solved my problem :)

        Now that you wrote it, i remember how it worked in c++. Only one cpp file QVector<gUser> Users; and others extern QVector<gUser> Users; for access.

        Thanks!

        1 Reply Last reply
        0

        3/3

        25 Oct 2022, 05:16

        • Login

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