Results 1 to 5 of 5

Thread: Problem with method call inside a constructor.

  1. #1
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Problem with method call inside a constructor.

    Good day!

    I created a class which, at the moment, i have one method beside the constructor/destructor:
    Qt Code:
    1. //signature
    2. "QVector<int> insertNeatly(int num, QVector<int> raffle, int idRfl)"
    To copy to clipboard, switch view to plain text mode 

    In the constructor i'm trying to call this method but i'm having the following error message, but first, the part of my code:
    Qt Code:
    1. //...code in the constructor
    2. //...
    3. QVector< int > raffle(idRfl, 0);
    4. for(int i = 0; i < idRfl; i++)
    5. {
    6. //Error here-> raffle = insertNeatly(line.at( i ).toInt( ), raffle, idRfl);//"line" is a stringlist.
    7. }
    8. //...
    To copy to clipboard, switch view to plain text mode 

    Error message:
    D:\Programming\C-C++\Qt\Windows\historic.cpp:26: error: undefined reference to `Historic::insertNeatly(int, QVector<int>, int)'
    I don't know if i'm right but i believe, since this code are in constructor, i cannot call other method of this class in it. Is that right?

    If i'm right, what do you think i create another class just to put this method so i can call him in my constructor? Do you have any other idea?

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with method call inside a constructor.

    Quote Originally Posted by robgeek View Post
    I don't know if i'm right but i believe, since this code are in constructor, i cannot call other method of this class in it. Is that right?
    No, you can call any method of a class inside its constructor.

    Your error is also not a compiler error (which would indicate something C++ doesn't allow), it is a linker error.

    undefined reference applied on a method usually means that this method is missing its implementation.
    I.e. it is declared (e.g. in the header) but not implemented

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    robgeek (6th April 2015)

  4. #3
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: Problem with method call inside a constructor.

    As i showed here the signature you can see i forgot the "Class::", now is working fine.

    Thank you!

  5. #4
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: Problem with method call inside a constructor.

    You're passing and returning that QVector by value, so it's being copied each time the loop iterates. Unless that is the desired behaviour, passing it as a pointer or reference should make that loop faster.
    Qt Code:
    1. "QVector<int>& Historic::insertNeatly(int num, QVector<int>& raffle, int idRfl)"
    To copy to clipboard, switch view to plain text mode 
    Then the call to it would remain: raffle = insertNeatly( line.at( i ).toInt( ), raffle, idRfl );

    EDIT: QVector actually uses "implicit sharing," so you're copying references to the same data. I think using the same reference will prevent it from being copied-on-write as inside that loop you're using, but I'd wait for the opinion of someone more experienced.
    http://doc.qt.io/qt-5/implicit-shari...ist-of-classes
    Last edited by Kryzon; 7th April 2015 at 00:59.

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with method call inside a constructor.

    Quote Originally Posted by Kryzon View Post
    EDIT: QVector actually uses "implicit sharing," so you're copying references to the same data. I think using the same reference will prevent it from being copied-on-write as inside that loop you're using, but I'd wait for the opinion of someone more experienced.
    http://doc.qt.io/qt-5/implicit-shari...ist-of-classes
    The vector detaches (creates a full or deep copy) when it has more than one reference (the case here when passed by value) and any of its non-const methods is called.
    So if the function only calls const methods, no data copying will take place.

    In Qt methods containers like that are usually passed by const-reference into functions and returned by value.

    In this special case, where the input is the result, i.e. the input container is effectively the one being inserted into, it would make most sense to make the argument an input/output parameter (using pointer or reference as suggested) and not have a return value at all.

    Cheers,
    _

Similar Threads

  1. QGraphicsItem: pure virtual method call problem
    By phuongot in forum Qt Programming
    Replies: 5
    Last Post: 17th January 2012, 07:35
  2. Replies: 1
    Last Post: 27th June 2011, 21:37
  3. load table inside constructor
    By sattu in forum Qt Programming
    Replies: 0
    Last Post: 10th January 2011, 10:09
  4. Call Constructor within Designer
    By NoRulez in forum Qt Programming
    Replies: 22
    Last Post: 24th June 2009, 08:27
  5. Constructor call problem
    By MarkoSan in forum Qt Programming
    Replies: 22
    Last Post: 28th May 2008, 14:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.