PDA

View Full Version : QVector of structures without default constructor



AD
8th July 2009, 08:39
Hello. I have stucture without default constructor.

struct LOGRECORD
{/*.....................*/};

class M
{
QVector<LOGRECORD> log;

M();
/*.....................*/
};

M::M(): log(0, LOGRECORD(/*parameters*/))
{}

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?

nish
8th July 2009, 09:29
just give it a empty default constructor...
and remove the '0' in log(0,...); make it at least 1

lyuts
8th July 2009, 13:42
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:



QVector ( int size, const T & value )


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


M::M()
{
LOGRECORD logRec;
log(1, logRec);
}

nish
8th July 2009, 13:49
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:



QVector ( int size, const T & value )


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


M::M()
{
LOGRECORD logRec;
log(1, logRec);
}


by the way you cannot do like
log(1, logRec);
because as far as i remember, c++ dont allow to call the constructor:)

lyuts
8th July 2009, 13:56
... c++ dont allow to call the constructor:)

QVector has the following constructor

QVector ( int size )

Are you saying that I cannot do this


QVector vec = QVector(10);


?????

nish
8th July 2009, 16:55
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..


myclass
{
myclass() : v(2)//here it is ok at constructor initializer list
{
}
myclass()
{
v(2);//you cannnot do it this way...
}

QVector v;
}


your code did the same thing... rite??


M::M()
{
LOGRECORD logRec;
log(1, logRec);//ERROR
}


hope i am clear now!!

lyuts
8th July 2009, 21:28
Oh, got it what you mean.