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. call to implicitly-deleted copy constructor
Forum Updated to NodeBB v4.3 + New Features

call to implicitly-deleted copy constructor

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 8.9k Views 2 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.
  • C Offline
    C Offline
    Claudio Varini
    wrote on 28 Apr 2020, 15:42 last edited by Claudio Varini
    #1

    Hi guys
    this code generates an error "call to implicitly-deleted copy constructor"

    // profit.h
    
    class Profit : public QObject
    {
        Q_OBJECT
    
    public:
        explicit Profit(QObject *parent = nullptr);
    Q_INVOKABLE Profit parse(QString string);
    }
    
    // profit.cpp
    Profit Profit::parse(QString string)
    {
        QStringList pieces = string.split( ";" );
        Profit profit;
        return profit; // error here
    }
    

    Any idea how to fix that? Many thanks

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 28 Apr 2020, 15:58 last edited by
      #2

      Hi,

      QObject based class are not copiable.

      You have to create an instance on the heap and return the corresponding pointer.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • C Offline
        C Offline
        Claudio Varini
        wrote on 29 Apr 2020, 14:07 last edited by
        #3

        Thanks do you mean something like that? How to create it in the heap?

        Profit * Profit::parse(QString string)
        {
        QStringList pieces = string.split( ";" );
        Profit profit;
        profit.setDate(pieces.takeAt(0));
        profit.setMoney(pieces.takeAt(1).toDouble());
        return &profit;
        }

        Many thanks

        1 Reply Last reply
        1
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 29 Apr 2020, 14:10 last edited by
          #4

          Not at all, you are returning a reference to local stack allocated object which means it's dangling as soon as you are back on the other side of the parse call because profit is destroyed when the method ends.

          Heap allocation: Profit *profit = new Profit;

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 29 Apr 2020, 14:16 last edited by
            #5

            Hi
            but it looks a bit odd.
            Why does Profit's parse return another Profit instance?

            I would expect something like

            
            Profit p;
            p.parse("...");
            p.getDate() // use for something
            p.GetMoney()
            
            and not
            Profit p;
            Profit  *result = p.parse("...");
            result ->getDate()
            result ->GetMoney()
            delete result;// else you leak
            }
            
            1 Reply Last reply
            2

            1/5

            28 Apr 2020, 15:42

            • Login

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