Solved Simple question about QVector append for pointers
-
Hi,
I have a QVector and I want to append elements into it. What is the correct way? I get always compiler errors.
I dont want to post errors, I need only correct syntax :).h file:
private: QVector<SystemUnit> SystemUnits; int AddElement(SystemUnit *SU); int RemoveElement(int index);
.cpp file:
int Facility::AddElement(SystemUnit *SU) { SystemUnits.append(&SU); // This syntax is absolutly wrong and I get compiler errors. return 0; }
How to append in a QVector from a pointer object?
Thanks.
-
@kahlenberg Your code is wrong:
QVector<SystemUnit> SystemUnits;
SystemUnits is a vector for SystemUnit not pointers to SystemUnit. It should be:
QVector<SystemUnit*> SystemUnits;
int Facility::AddElement(SystemUnit *SU) { // Since SU is already a pointer you do not need &, just fix SystemUnit like described above SystemUnits.append(&SU); // This syntax is absolutly wrong and I get compiler errors. return 0; }
-
@kahlenberg said:
SystemUnits.append(&SU); // This syntax is absolutly wrong and I get compiler errors.
The dereferencing operator in C++ is
*
not&
Depends what you want to do here:
- to store a copy of SU (not SU itself) use
SystemUnits.append(*SU);
- to store the same item SU is pointing to then declare
QVector<SystemUnit*> SystemUnits;
and useSystemUnits.append(SU);
- to store a copy of SU (not SU itself) use