Results 1 to 7 of 7

Thread: QVector of structures without default constructor

  1. #1
    Join Date
    Jun 2008
    Location
    Saint-Petersburg
    Posts
    50
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default QVector of structures without default constructor

    Hello. I have stucture without default constructor.
    Qt Code:
    1. struct LOGRECORD
    2. {/*.....................*/};
    3.  
    4. class M
    5. {
    6. QVector<LOGRECORD> log;
    7.  
    8. M();
    9. /*.....................*/
    10. };
    11.  
    12. M::M(): log(0, LOGRECORD(/*parameters*/))
    13. {}
    To copy to clipboard, switch view to plain text mode 
    But compiler had next errors:
    1>Compiling...
    1>VerticalGraphics.cpp
    1>tlv.cpp
    1>c:\qt\4.3.2\include\qtcore\../../src/corelib/tools/qvector.h(410) : error C2512: 'LOGRECORD' : no appropriate default constructor available
    1> c:\qt\4.3.2\include\qtcore\../../src/corelib/tools/qvector.h(396) : while compiling class template member function 'void QVector<T>::realloc(int,int)'
    1> with
    1> [
    1> T=LOGRECORD
    1> ]
    1> c:\qt\4.3.2\include\qtcore\../../src/corelib/tools/qvector.h(371) : while compiling class template member function 'QVector<T>::QVector(int,const T &)'
    1> with
    1> [
    1> T=LOGRECORD
    1> ]
    1> d:\projects\src\transaslogviewer\ParamDescr.h(39) : see reference to class template instantiation 'QVector<T>' being compiled
    1> with
    1> [
    1> T=LOGRECORD
    1> ]
    1>c:\qt\4.3.2\include\qtcore\../../src/corelib/tools/qvector.h(451) : error C2512: 'LOGRECORD' : no appropriate default constructor available
    1>Reader.cpp
    1>c:\qt\4.3.2\include\qtcore\../../src/corelib/tools/qvector.h(410) : error C2512: 'LOGRECORD' : no appropriate default constructor available
    1> c:\qt\4.3.2\include\qtcore\../../src/corelib/tools/qvector.h(396) : while compiling class template member function 'void QVector<T>::realloc(int,int)'
    1> with
    1> [
    1> T=LOGRECORD
    1> ]
    1> c:\qt\4.3.2\include\qtcore\../../src/corelib/tools/qvector.h(371) : while compiling class template member function 'QVector<T>::QVector(int,const T &)'
    1> with
    1> [
    1> T=LOGRECORD
    1> ]
    1> d:\projects\src\transaslogviewer\ParamDescr.h(39) : see reference to class template instantiation 'QVector<T>' being compiled
    1> with
    1> [
    1> T=LOGRECORD
    1> ]
    1>c:\qt\4.3.2\include\qtcore\../../src/corelib/tools/qvector.h(451) : error C2512: 'LOGRECORD' : no appropriate default constructor available
    1>Generating Code...
    1>Build log was saved at "file://d:\Projects\Win32\Obj\Debug\TransasLogViewer\Build Log.htm"
    1>TLV - 4 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    What can I correct there errors?
    Last edited by AD; 8th July 2009 at 07:49.

  2. #2
    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

  3. #3
    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.

  4. #4
    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

  5. #5
    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.

  6. #6
    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.

  7. #7
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.