Results 1 to 7 of 7

Thread: QVector of structures without default constructor

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QVector of structures without default constructor

    just give it a empty default constructor...
    and remove the '0' in log(0,...); make it at least 1

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QVector of structures without default constructor

    The compiler generates a default constructor for your struct (just like with classes) if it is not written manually.

    The problem with your case is that QVector's constructor looks like this:

    Qt Code:
    1. QVector ( int size, const T & value )
    To copy to clipboard, switch view to plain text mode 

    It means you have to pass value by reference. The way you do this, will not return a reference.

    That's why you need to do
    Qt Code:
    1. M::M()
    2. {
    3. LOGRECORD logRec;
    4. log(1, logRec);
    5. }
    To copy to clipboard, switch view to plain text mode 
    I'm a rebel in the S.D.G.

  3. #3
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QVector of structures without default constructor

    Quote Originally Posted by lyuts View Post
    The compiler generates a default constructor for your struct (just like with classes) if it is not written manually.

    The problem with your case is that QVector's constructor looks like this:

    Qt Code:
    1. QVector ( int size, const T & value )
    To copy to clipboard, switch view to plain text mode 

    It means you have to pass value by reference. The way you do this, will not return a reference.

    That's why you need to do
    Qt Code:
    1. M::M()
    2. {
    3. LOGRECORD logRec;
    4. log(1, logRec);
    5. }
    To copy to clipboard, switch view to plain text mode 
    by the way you cannot do like
    log(1, logRec);
    because as far as i remember, c++ dont allow to call the constructor

  4. #4
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QVector of structures without default constructor

    Quote Originally Posted by MrDeath View Post
    ... c++ dont allow to call the constructor
    QVector has the following constructor
    Qt Code:
    1. QVector ( int size )
    To copy to clipboard, switch view to plain text mode 

    Are you saying that I cannot do this
    Qt Code:
    1. QVector vec = QVector(10);
    To copy to clipboard, switch view to plain text mode 

    ?????
    I'm a rebel in the S.D.G.

  5. #5
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QVector of structures without default constructor

    there is a difference in declaration and defination...
    when a constructor is called, the members are defined... the member's constructors are called
    automatically... if you want to initialize members via their cotr.. then u have to use initializer list..

    Qt Code:
    1. myclass
    2. {
    3. myclass() : v(2)//here it is ok at constructor initializer list
    4. {
    5. }
    6. myclass()
    7. {
    8. v(2);//you cannnot do it this way...
    9. }
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    your code did the same thing... rite??
    Qt Code:
    1. M::M()
    2. {
    3. LOGRECORD logRec;
    4. log(1, logRec);//ERROR
    5. }
    To copy to clipboard, switch view to plain text mode 

    hope i am clear now!!
    Last edited by nish; 8th July 2009 at 16:02.

  6. #6
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QVector of structures without default constructor

    Oh, got it what you mean.
    I'm a rebel in the S.D.G.

Similar Threads

  1. default parameters in constructor class
    By mickey in forum General Programming
    Replies: 4
    Last Post: 23rd February 2008, 18:44
  2. Q3Frame : no appropraite default constructor available
    By Project25 in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2007, 23:23
  3. What default constructor?
    By bitChanger in forum General Programming
    Replies: 5
    Last Post: 15th February 2006, 19:50

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
  •  
Qt is a trademark of The Qt Company.