Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

setting static struct and read



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

  • Lifetime Qt Champion

    Hi,

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

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



  • 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
    


  • 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
    


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



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



  • @elicat said in setting static struct and read:

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

    DichiarativeGlobali::in={1,10};


  • Lifetime Qt Champion

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



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



  • @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);
    	
    }
    


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


Log in to reply