setting static struct and read
-
wrote on 29 Aug 2018, 11:08 last edited by
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;
-
Hi,
You just declared the
struct
itself. There's object of that type anywhere.In any case, what are the motivation behind that design ?
-
wrote on 29 Aug 2018, 12:35 last edited by mpergand
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
-
wrote on 29 Aug 2018, 12:50 last edited by
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
-
Hi,
You just declared the
struct
itself. There's object of that type anywhere.In any case, what are the motivation behind that design ?
wrote on 29 Aug 2018, 13:59 last edited by@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
-
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
wrote on 29 Aug 2018, 14:32 last edited by@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:
-
@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:
wrote on 29 Aug 2018, 16:36 last edited by mpergand@elicat said in setting static struct and read:
I want init the static structure when i call a method settingConnectDb
DichiarativeGlobali::in={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
@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.
-
wrote on 30 Aug 2018, 06:18 last edited by
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 ? :-)
-
@elicat said in setting static struct and read:
I want init the static structure when i call a method settingConnectDb
DichiarativeGlobali::in={1,10};
wrote on 30 Aug 2018, 06:30 last edited by elicat@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:
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); }
-
wrote on 30 Aug 2018, 10:13 last edited by mpergand
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/11