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

Array object constructor error



  • Array object constructor error.
    At runtime, the value of the array is determined,
    I do not know how to solve this problem.

        int setRoom = 9;
        RoomButton *room = new RoomButton[setRoom];  // RoomButton Inheritance QPushButton
        QString roomNum[setRoom];
        roomNum[0] = "201";
        roomNum[1] = "202";
        roomNum[2] = "203";
        roomNum[3] = "205";
        roomNum[4] = "206";
        roomNum[5] = "207";
        roomNum[6] = "208";
        roomNum[7] = "209";
        roomNum[8] = "210";
    
        for(int ii =0; ii<3; ii++){
            for (int i =0; i<3; i++){
                ui->roomSeletGLayout->addWidget(&room[ii*3+i],ii,i); // Good work!
                room[ii*3+i]->setText(roomNum[ii*3+i]); // This is error...T^T
            }
        }
    
    

    Thanks in advance for your help.



  • @Kycho

    What is the exact error message?

    Note, dynamic size of arrays as used there requires at least C++ standard of 99 or more. What are your compile settings?



  • @koahnig

    Thank you for your reply.

    Error message is as follows.

    /home/jiminkim/QtProject/SVD110_Tesst/mainwindow.cpp:30: error: base operand of ‘->’ has non-pointer type ‘RoomButton’
                 room[ii*3+i]->setText(roomNum[ii*3+i]); // it is error...T^T
                             ^~
    
    /home/jiminkim/QtProject/SVD110_Tesst/mainwindow.cpp:30: error: member reference type 'RoomButton' is not a pointer; did you mean to use '.'?
    

    I do not know if C ++ 99 or later is correct....
    But recently installed.
    Using compiler (Linux Ubuntu)
    0_1543312726972_스크린샷, 2018-11-27 18-57-42.png



  • @Kycho

    You would need a CONFIG settings like

    CONFIG += c++11
    

    for C++11 standard. Depending on your actual compiler version this might be already default.

    The syntax of

     RoomButton *room = new RoomButton[setRoom]; 
    

    is wrong for you try to do. It should be more like

     RoomButton *room[setRoom];
    for ( int i = 0; i < setRoom; ++i )
        RoomButton[i]= new RoomButton; 
    

    However, I recommend using a container such as std::vector or QVector for handling this.

    Note: above source is brain to keyboard not tested.



  • @koahnig

    Thank you!!!!!!!!!!!!!!!!!!


Log in to reply