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

Global QVector struct how to?

Scheduled Pinned Locked Moved Solved General and Desktop
globalqvectorstruct
3 Posts 2 Posters 506 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 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!

    JonBJ 1 Reply Last reply
    0
    • Q qDebug

      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!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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 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

        • Login

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