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. struct: constructor/destructor for fixing C2280 error: attempting to reference a deleted function
Forum Updated to NodeBB v4.3 + New Features

struct: constructor/destructor for fixing C2280 error: attempting to reference a deleted function

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 1.7k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    sitesv
    wrote on 8 Jul 2020, 17:00 last edited by
    #1

    Hello!
    I got a C2280 error with next code:

    struct my_struct{
        QTcpSocket sock;
    };
    std::vector<my_struct> my_vector;
    
    int main(int argc, char *argv[]){
        QCoreApplication a(argc, argv);
        for(int i = 0; i < 10; i++){
            my_struct mtca;
            my_vector.push_back(mtca);
        }
        return a.exec();
    }
    

    There is an error with the copying objects to the vector.
    I got the same error when using a QMutex in this struct.
    As I understood, I need to use pointers.

    struct my_struct{
        QTcpSocket *sock = nullptr;
        my_struct(){
            sock = new QTcpSocket;
        }
    };
    std::vector<my_struct> my_vector;
    int main(int argc, char *argv[]){
        QCoreApplication a(argc, argv);
        for(int i = 0; i < 10; i++){
            my_struct mtca;
            my_vector.push_back(mtca);
        }
        return a.exec();
    }
    

    How to work correctly with a dynamic creation and destroying objects in the structs?
    Thank you!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 8 Jul 2020, 17:42 last edited by
      #2

      QObjects can not be copied. Use a pointer (or smart pointer) in your struct.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 8 Jul 2020, 17:45 last edited by
        #3

        Hi
        Anything that inherits QObject cannot be copied as that would make signal and slot go amok.

        So easy fix is to use pointers to such members

        struct my_struct {
            QTcpSocket *sock;
            my_struct() : sock(new QTcpSocket ) {}  // let constructor allocate it.
            ~my_struct()
            {
                sock->deleteLater();   // remember to deallocate it
            }
        };
        
        

        oh Mr Ehrlicher beat me to it :)

        1 Reply Last reply
        3

        1/3

        8 Jul 2020, 17:00

        • Login

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