struct and passing array parameters



  • /* file dichiarative.h */
    #ifndef DICHIARATIVE_H
    #define DICHIARATIVE_H
    #include <QString>
    
    class dichiarative {
    
    public:
        dichiarative();
    
        struct paramingresso {
            static QString arg_db_type;
            static QString arg_db_driver;
            static QString arg_db_name;
            static QString arg_db_path;
        };
    
        void settingDichiarative(char* argomenti);
    };
    
    #endif // DICHIARATIVE_H
    
    /* file dichiarative.cpp */
    #include "dichiarative.h"
    dichiarative::dichiarative()
    {
    
    }
    
    void dichiarative::settingDichiarative(char* argomenti)
    {
        dichiarative::paramingresso::arg_db_type = QStringLiteral("%1").arg(argomenti[1];
        dichiarative::paramingresso::arg_db_driver = QStringLiteral("%1").arg(argomenti[2];
        dichiarative::paramingresso::arg_db_name = QStringLiteral("%1").arg(argomenti[3];
        dichiarative::paramingresso::arg_db_path = QStringLiteral("%1").arg(argomenti[4];
    }
    
    /* file main.cpp */
    int main(int argc, char *argv[])
    {
        void dichiarative::settingDichiarative(argv);
    
       QString pippo = dichiarative::paramingresso.arg_db_driver;
    ..
    }
    
    

    Please help me. I'm beginner in C++.
    Why i have this two error?

    1. main.cpp: In function 'int qMain(int, char**)':
      main.cpp:11:43: error: qualified-id in declaration before '(' token
      void dichiarative::settingDichiarative(argv);
      ^
    2. main.cpp:13:47: error: expected primary-expression before '.' token
      QString pippo = dichiarative::paramingresso.arg_db_driver;
      ^

    For error 1 I have try "cahr* argv", "*argv", "argv[]" but everyone whith different error.

    Thanks.
    Gianfranco


  • Moderators

    @Digitale_GFacchini said in struct and passing array parameters:

    void dichiarative::settingDichiarative(argv);

    You have not created an object. Try this:

    dichiarative myObject;
    myObject.settingDichiarative(argv);
    QString pippo = myObject.paramingresso.arg_db_driver;
    


  • @Digitale_GFacchini said in struct and passing array parameters:

    dichiarative

    Sorry sierdzio, but dichiarative it's a class. the class is a object. or no?


  • Lifetime Qt Champion

    Hi,

    No, a class is a definition used to create objects AKA "instances".



  • @sierdzio said in struct and passing array parameters:

    dichiarative myObject;
    myObject.settingDichiarative(argv);

    ok so, i i want use a classe a need a istance.
    Now i have a problem in parameter why? I have declared :

    char* argv
    

    into in file header and in file cpp where is the class dichiarative and his method

        dichiarative myObject;
        myObject.settingDichiarative(argv);
    

    main.cpp:12:38: error: no matching function for call to 'dichiarative::settingDichiarative(char**&)'
    myObject.settingDichiarative(argv);
    ^



  • Ok hai have put:

    myObject.settingDichiarative(*argv);
    

    And so it's ok, but now i have error in method settingDichiarative.

    C:\Generale\OneDrive - ELICAT SRL - 03978700288\Developer\QtApplicazioni\youtube_and_web\EVILEG\SQLiteDbAcccesListModel/dichiarative.cpp:14: undefined reference to `dichiarative::paramingresso::arg_db_name'
    

    This error for every idem of structure declared into .h

    struct paramingresso {
            static QString arg_db_type;
            static QString arg_db_driver;
            static QString arg_db_name;
            static QString arg_db_path;
        };
    

    and the method is :

    void dichiarative::settingDichiarative(char* argomenti)
    {
        dichiarative::paramingresso::arg_db_type = QStringLiteral("%1").arg(argomenti[1]);
        dichiarative::paramingresso::arg_db_driver = QStringLiteral("%1").arg(argomenti[2]);
        dichiarative::paramingresso::arg_db_name = QStringLiteral("%1").arg(argomenti[3]);
        dichiarative::paramingresso::arg_db_path = QStringLiteral("%1").arg(argomenti[4]);
    }
    

    In this monts i have sty but it's too much arguments and every time I try to apply I block myself for errors .. Be patient then when I understand bele the logic I will be able to reciprocate. At least I hope


  • Moderators

    This is - again - because you have no object / variable of type "paramingresso". In your "dichiarative" class, add this:

    public:
      paramingresso myStruct;
    

    And now modify your method like so:

    void dichiarative::settingDichiarative(char* argomenti)
    {
        myStruct.arg_db_type = QStringLiteral("%1").arg(argomenti[1]);
        myStruct.arg_db_driver = QStringLiteral("%1").arg(argomenti[2]);
        myStruct.arg_db_name = QStringLiteral("%1").arg(argomenti[3]);
        myStruct.arg_db_path = QStringLiteral("%1").arg(argomenti[4]);
    }
    

    Also, as a side note - it's much easier to extract command line arguments using Qt methods. Check out https://doc.qt.io/qt-5/qcoreapplication.html#arguments or (a bit more advanced) https://doc.qt.io/qt-5/qcommandlineparser.html#details



  • Thanks for reference to extract command line. however I continue as an exercise with reading argv and setting up a structure. Are you telling me to instantiate the structure in the header file? If I understand correctly the result is:

    undefined reference to `DichiarativeGlobali::paramingresso::arg_db_type'
    undefined reference to `DichiarativeGlobali::paramingresso::arg_db_driver'
    undefined reference to `DichiarativeGlobali::paramingresso::arg_db_name'
    undefined reference to `DichiarativeGlobali::paramingresso::arg_db_path'
    

    P:S. I have changed aold name class in DichiarativeGlobali.



  • @Digitale_GFacchini said in struct and passing array parameters:

    Ok hai have put:
    myObject.settingDichiarative(*argv);

    This is wrong, you should change settingDichiarative(char* argomenti) to settingDichiarative(char** argomenti) and call myObject.settingDichiarative(argv);.



  • Thanks VRonin but .. . Why? in main.cpp i have :

    int main(int argc, char *argv[])
    

    What difference from * to ** ??


  • Moderators

    @Digitale_GFacchini said in struct and passing array parameters:

    char *argv[]

    this is same as "char **argv".
    You should read a book or tutorial about C++ basics.



  • thanks VRonin,
    topic of * is "pointers". But why two ** ??
    Do you know any good books in Italian? I'm reading everything in English and I was not using it for years.


  • Lifetime Qt Champion

    char * = array of char
    char ** = array of array of char



  • Thank SGaist


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.