QVector of structures without default constructor
Hello. I have stucture without default constructor.
Code:
struct LOGRECORD
{/*.....................*/};
class M
{
QVector<LOGRECORD> log;
M();
/*.....................*/
};
M::M(): log(0, LOGRECORD(/*parameters*/))
{}
But compiler had next errors:
Quote:
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?
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
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:
Code:
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
Code:
M::M()
{
LOGRECORD logRec;
log(1, logRec);
}
Re: QVector of structures without default constructor
Quote:
Originally Posted by
lyuts
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:
Code:
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
Code:
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:)
Re: QVector of structures without default constructor
Quote:
Originally Posted by
MrDeath
... c++ dont allow to call the constructor:)
QVector has the following constructor
Are you saying that I cannot do this
?????
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..
Code:
myclass
{
myclass() : v(2)//here it is ok at constructor initializer list
{
}
myclass()
{
v(2);//you cannnot do it this way...
}
}
your code did the same thing... rite??
Code:
M::M()
{
LOGRECORD logRec;
log(1, logRec);//ERROR
}
hope i am clear now!!
Re: QVector of structures without default constructor
Oh, got it what you mean.