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. setting static struct and read

setting static struct and read

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 2.1k 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.
  • E Offline
    E Offline
    elicat
    wrote on 29 Aug 2018, 11:08 last edited by
    #1

    Hello, Today another question:
    I have create a class for a static structure with i want setting parameter for connection db and after read this value in other contest of the program. When i compiled i have the following error, where i wrong?

    1>dichiarative.obj : error LNK2001: unresolved external symbol "public: static int DichiarativeGlobali::parametriAccessDb::arg_db_type" (?arg_db_type@parametriAccessDb@DichiarativeGlobali@@2HA)
    1>dichiarative.obj : error LNK2001: unresolved external symbol "public: static class QString DichiarativeGlobali::parametriAccessDb::arg_db_driver" (?arg_db_driver@parametriAccessDb@DichiarativeGlobali@@2VQString@@A)
    1>dichiarative.obj : error LNK2001: unresolved external symbol "public: static class QString DichiarativeGlobali::parametriAccessDb::arg_user_db" (?arg_user_db@parametriAccessDb@DichiarativeGlobali@@2VQString@@A)
    1>dichiarative.obj : error LNK2001: unresolved external symbol "public: static class QString DichiarativeGlobali::parametriAccessDb::arg_pass_db" (?arg_pass_db@parametriAccessDb@DichiarativeGlobali@@2VQString@@A)
    1>dichiarative.obj : error LNK2001: unresolved external symbol "public: static class QString DichiarativeGlobali::parametriAccessDb::arg_hostname_db" (?arg_hostname_db@parametriAccessDb@DichiarativeGlobali@@2VQString@@A)
    1>dichiarative.obj : error LNK2001: unresolved external symbol "public: static class QString DichiarativeGlobali::parametriAccessDb::arg_port_db" (?arg_port_db@parametriAccessDb@DichiarativeGlobali@@2VQString@@A)
    1>dichiarative.obj : error LNK2001: unresolved external symbol "public: static class QString DichiarativeGlobali::parametriAccessDb::arg_connoption_db" (?arg_connoption_db@parametriAccessDb@DichiarativeGlobali@@2VQString@@A)
    

    I copy a code file dichiarative.h

    class DichiarativeGlobali
    {
    public:
         DichiarativeGlobali();
    
    	 struct parametriAccessDb {
    		 static int arg_db_type;
    		 static QString arg_db_driver;
    		 static QString arg_user_db;
    		 static QString arg_pass_db;
    		 static QString arg_hostname_db;
    		 static QString arg_port_db;
    		 static QString arg_connoption_db;
    	 };
    
       void settingConnectDb(QVector<QString> settingaccessdb);
    
    private:
    
    };
    

    this's code dichiarative.cpp

    DichiarativeGlobali::DichiarativeGlobali()
    {
    
    }
    void DichiarativeGlobali::settingConnectDb(QVector<QString> settingaccessdb)
    {
    
    	DichiarativeGlobali::parametriAccessDb::arg_db_type  = settingaccessdb.value(0).toInt();
    	DichiarativeGlobali::parametriAccessDb::arg_db_driver = settingaccessdb.value(1);
    	DichiarativeGlobali::parametriAccessDb::arg_user_db = settingaccessdb.value(2);
    	DichiarativeGlobali::parametriAccessDb::arg_pass_db = settingaccessdb.value(3);
    	DichiarativeGlobali::parametriAccessDb::arg_hostname_db = settingaccessdb.value(4);
    	DichiarativeGlobali::parametriAccessDb::arg_port_db = settingaccessdb.value(5);
    	DichiarativeGlobali::parametriAccessDb::arg_connoption_db = settingaccessdb.value(6);
    
    }
    

    Here when i setting parameter of the structure:

    	DichiarativeGlobali  *dichiarative = new DichiarativeGlobali;
    
    	QVector<QString> settingDbConnection;
    	settingDbConnection.append("0");		// db_type
    	settingDbConnection.append("QODBC");		// db_driver
    	settingDbConnection.append("");			// arg_user_db
    	settingDbConnection.append("");			// arg_pass_db
    	settingDbConnection.append("");			// arg_hostname_db
    	settingDbConnection.append("");			// arg_port_db
    	settingDbConnection.append("");			// arg_conn_db
    	// imposto i parametri
    	dichiarative->settingConnectDb(settingDbConnection);
    

    and here when i want read value of the structure ( I include file dichiarative.h):

    QString driver = DichiarativeGlobali::parametriAccessDb::arg_db_driver;
    

    Saluti, Gianfranco Elicat

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 29 Aug 2018, 11:18 last edited by
      #2

      Hi,

      You just declared the struct itself. There's object of that type anywhere.

      In any case, what are the motivation behind that design ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      E 1 Reply Last reply 29 Aug 2018, 13:59
      2
      • M Offline
        M Offline
        mpergand
        wrote on 29 Aug 2018, 12:35 last edited by mpergand
        #3

        static variables must be defined.
        example:

        class Base
        {
        public:
        
        struct Inner {
                 static int type;
                 };
        };
        
        int Base::Inner::type=1;  // defined/initialized here
        
        int main(int argc, char *argv[])
        {
        qDebug()<<Base::Inner::type;  // print 1
        
        1 Reply Last reply
        4
        • M Offline
          M Offline
          mpergand
          wrote on 29 Aug 2018, 12:50 last edited by
          #4

          As a better alternative i think, you can use a static inner struct:

          class Base
          {
          public:
          
          struct Inner {
                   int type;
                   int val;
                   };
          
          
          static Inner in;  // declared as static 
          
          };
          
          Base::Inner Base::in{1,10}; // define and init the struct
          
          int main(int argc, char *argv[])
          {
          qDebug()<<Base::in.type<<Base::in.val;  // print 1 10
          
          E 1 Reply Last reply 29 Aug 2018, 14:32
          1
          • S SGaist
            29 Aug 2018, 11:18

            Hi,

            You just declared the struct itself. There's object of that type anywhere.

            In any case, what are the motivation behind that design ?

            E Offline
            E Offline
            elicat
            wrote on 29 Aug 2018, 13:59 last edited by
            #5

            @SGaist said in setting static struct and read:

            There's object of that type anywhere.

            I need a method for load parameter for my connection db in more position

            Saluti, Gianfranco Elicat

            S 1 Reply Last reply 29 Aug 2018, 19:47
            0
            • M mpergand
              29 Aug 2018, 12:50

              As a better alternative i think, you can use a static inner struct:

              class Base
              {
              public:
              
              struct Inner {
                       int type;
                       int val;
                       };
              
              
              static Inner in;  // declared as static 
              
              };
              
              Base::Inner Base::in{1,10}; // define and init the struct
              
              int main(int argc, char *argv[])
              {
              qDebug()<<Base::in.type<<Base::in.val;  // print 1 10
              
              E Offline
              E Offline
              elicat
              wrote on 29 Aug 2018, 14:32 last edited by
              #6

              @mpergand said in setting static struct and read:

              Base::Inner Base::in{1,10}; // define and init the struct

              Hello,
              I have put your simple example into my class with name DichiarativeGlobali.
              I want init the static structure when i call a method settingConnectDb because the value i don't know when i start program.
              Inside the method i have this error:
              0_1535553169832_statistructurexforum.jpg

              Saluti, Gianfranco Elicat

              M 1 Reply Last reply 29 Aug 2018, 16:36
              0
              • E elicat
                29 Aug 2018, 14:32

                @mpergand said in setting static struct and read:

                Base::Inner Base::in{1,10}; // define and init the struct

                Hello,
                I have put your simple example into my class with name DichiarativeGlobali.
                I want init the static structure when i call a method settingConnectDb because the value i don't know when i start program.
                Inside the method i have this error:
                0_1535553169832_statistructurexforum.jpg

                M Offline
                M Offline
                mpergand
                wrote on 29 Aug 2018, 16:36 last edited by mpergand
                #7

                @elicat said in setting static struct and read:

                I want init the static structure when i call a method settingConnectDb

                DichiarativeGlobali::in={1,10};

                E 1 Reply Last reply 30 Aug 2018, 06:30
                0
                • E elicat
                  29 Aug 2018, 13:59

                  @SGaist said in setting static struct and read:

                  There's object of that type anywhere.

                  I need a method for load parameter for my connection db in more position

                  S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 29 Aug 2018, 19:47 last edited by
                  #8

                  @elicat said in setting static struct and read:

                  @SGaist said in setting static struct and read:

                  There's object of that type anywhere.

                  I need a method for load parameter for my connection db in more position

                  That doesn't sound good. Why do you need these parameters in more positions ? The QSqlDatabase class already supports retrieving connection informations.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • E Offline
                    E Offline
                    elicat
                    wrote on 30 Aug 2018, 06:18 last edited by
                    #9

                    My project need connect various database, select from user, in more point of program. So i need make more connection. The driver and the position database (local or network) depends on the configuration from the installation.
                    this parameter are setting when start program (argv) and i want use them into method where i connect database.

                    	         static int arg_db_type;
                    		 static QString arg_db_driver;
                    		 static QString arg_user_db;
                    		 static QString arg_pass_db;
                    		 static QString arg_hostname_db;
                    		 static QString arg_port_db;
                    		 static QString arg_connoption_db;
                    
                    

                    In any case, if I understand well the use of static variables (in this case a structure) is good for my training. Do not you think ? :-)

                    Saluti, Gianfranco Elicat

                    1 Reply Last reply
                    0
                    • M mpergand
                      29 Aug 2018, 16:36

                      @elicat said in setting static struct and read:

                      I want init the static structure when i call a method settingConnectDb

                      DichiarativeGlobali::in={1,10};

                      E Offline
                      E Offline
                      elicat
                      wrote on 30 Aug 2018, 06:30 last edited by elicat
                      #10

                      @mpergand said in setting static struct and read:

                      DichiarativeGlobali::in={1,10};

                      Hello Said,
                      I had also thought and I had tried but it presents me the following error:
                      0_1535610546689_statistructurexforum.jpg

                      Posto il codice attuale del file h:

                      class DichiarativeGlobali
                      {
                      public:
                      	DichiarativeGlobali();
                      
                      	struct parametriAccessDb {
                      		int arg_db_type;
                      		QString arg_db_driver;
                      		QString arg_user_db;
                      		QString arg_pass_db;
                      		QString arg_hostname_db;
                      		QString arg_port_db;
                      		QString arg_connoption_db;
                      	};
                      	
                      	static struct parametriAccessDb parametriDb;
                      
                          void settingConnectDb(QVector<QString> settingaccessdb);
                      
                      private:	
                      
                      };
                      

                      and this is cpp:

                      DichiarativeGlobali::DichiarativeGlobali()
                      {
                      }
                      void DichiarativeGlobali::settingConnectDb(QVector<QString> settingaccessdb)
                      {
                      	DichiarativeGlobali::parametriDb.arg_db_type = settingaccessdb.value(0).toInt();
                      	DichiarativeGlobali::parametriDb.arg_db_type  = settingaccessdb.value(0).toInt();
                      	DichiarativeGlobali::parametriDb.arg_db_driver = settingaccessdb.value(1);
                      	DichiarativeGlobali::parametriDb.arg_user_db = settingaccessdb.value(2);
                      	DichiarativeGlobali::parametriDb.arg_pass_db = settingaccessdb.value(3);
                      	DichiarativeGlobali::parametriDb.arg_hostname_db = settingaccessdb.value(4);
                      	DichiarativeGlobali::parametriDb.arg_port_db = settingaccessdb.value(5);
                      	DichiarativeGlobali::parametriDb.arg_connoption_db = settingaccessdb.value(6);
                      	
                      }
                      

                      Saluti, Gianfranco Elicat

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mpergand
                        wrote on 30 Aug 2018, 10:13 last edited by mpergand
                        #11

                        Seems you have not defined the struct in .cpp, add this at the head:

                        DichiarativeGlobali::parametriAccessDb DichiarativeGlobali::parametriDb;
                        

                        (sorry that's C++ :) )

                        and, since you init this variable in a method of the class, you can simply write:

                        parametriDb.arg_db_type= xxx;
                        

                        Warning:
                        Keep in mine that if you use a static variable (class variable), this variable is unique along all instances of the class,
                        In practice, it means there can only be one user defined in all your prog.
                        Seems ok for now, but If in some future you decide to manage several users, you will fail miserably ;)

                        My advice: use class/static variables with cautions.

                        1 Reply Last reply
                        1

                        1/11

                        29 Aug 2018, 11:08

                        • Login

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