[solved] QHash’s insert() wrong use?—> VS-Debugger showed botch data due to missing Qt-Addon for VS
Page |
1 |
Hello peoplez,
I have a beginner problem with my QHash
I built a controller, which handles my QHash. This Controller has got a method:
- {
- SpecialObject tPI (tID, tName, tCat, tCon); // tPI is set correctly
- this->myQHash.insert(tID, tPI); // wrong values inserted into myQHash
- }
The object tPI is correctly; all its attributes are correctly set.
The error is in the next line, where the key-SpecialObject-value pair (tID, tPI) should be inserted to myQHash. It is not real error, but rather an incorrect insertion.
For testing purposes, I have elsewhere:
- pDataController.addNewData(2511, tr("Home"), 20481, 1272923);
But according to my debugger following values are stored
(1, BadPtr, 262148, 17) instead of
(2511, tr(“Home”), 20481, 1272923)
Does anybody know which mistake I have done? I am thankful for any advise.
Cheers Huck
44 replies
Ok, and as far as I know every with new created pointer should be deleted by programmer later on.
- SpecialObject *tPI = new SpecialObject(tID, tName, tCat, tCon);
- this->myQHash.insert(tID, tPI);
- delete tPI;
- }
does not make any sence right? Shall I delete all those SpecialObjects pointers in the destructor later? How would you handle that?
A bit difficult to give advice with so little information. If SpecialObject is a small class, and it makes sense to pass SpecialObject objects around by value, then the code you have written should be OK. If not, then you must create the objects on the heap, using new, and then delete them when you no longer need them, e.g. when/before the QHash is destroyed.
@ludde: Yes at that moment SpecialObject has got one QString and 5 qint32 attributes.
However, this is going to be developed and thus rising.
@ZapB: Thatswhy I wrote an own class for it, I know that I need special format for output purposes late on. And thatswhy I haven’t take “struct”.
Anyway, creating my object on the heap does not solve my problem either. During debugging I can see botch values in pDataController as on the screenshot above. And same CXX0030 error.
Looks to me like you have not properly implemented the assignment and/or copy constructor of your SpecialObject class properly.@Hunger I thought QHash took a copy of objects when you insert them rather than using a reference to them as you are implying?
Correct!
To quote from the docs about container classes [doc.qt.nokia.com]
The values stored in the various containers can be of any assignable data type. To qualify, a type must provide a default constructor, a copy constructor, and an assignment operator. This covers most data types you are likely to want to store in a container, including basic types such as int and double, pointer types, and Qt data types such as QString, QDate, and QTime, but it doesn’t cover QObject or any QObject subclass (QWidget, QDialog, QTimer, etc.). If you attempt to instantiate a QList<QWidget>, the compiler will complain that QWidget’s copy constructor and assignment operators are disabled. If you want to store these kinds of objects in a container, store them as pointers, for example as QList<QWidget *>.
And more important:
If we don’t provide a copy constructor or an assignment operator, C++ provides a default implementation that performs a member-by-member copy.
Theses autogenerated operators and constructors are most likely not sufficient!
Here you have compileable but not yet linkable codes. LinkError see bottom.
DataItem.cpp
- #include "DataItem.h"
- #include <QString>
- DataItem::DataItem()
- {
- this->dataID = 0;
- this->dataCon = 0;
- this->dataCat = 0;
- this->dataName = "defName";
- }
- {
- this->dataID = tId;
- this->dataCon = tCon;
- this->dataCat = tCat;
- this->dataName = tName;
- }
- qint32 DataItem::getID() const { return this->dataID; }
- qint32 DataItem::getCat() const { return this->dataCat; }
- qint32 DataItem::getCon() const { return this->dataCon; }
- DataItem::~DataItem(void) {}
DataItem.h
- #pragma once
- #include <QString>
- class DataItem
- {
- public:
- DataItem();
- ~DataItem(void);
- qint32 getID() const;
- qint32 getCat() const;
- qint32 getCon() const;
- private:
- qint32 dataID;
- QString dataName;
- qint32 dataCat;
- qint32 dataCon;
- };
DataController.cpp
- #include <QHash>
- #include "DataController.h"
- #include "DataItem.h"
- //DataController::DataController(void)
- //{
- //
- //}
- //void DataController::addNew(DataItem * tPI) { this->myQHash.insert(tPI->getID(), *tPI); }
- {
- //DataItem tPI (tID, tName, tCat, tCon);
- DataItem * tPI = new DataItem(tID, tName, tCat, tCon);
- this->myQHash.insert(tID, tPI);
- }
- DataController::~DataController(void) {}
DataController.h
- #pragma once
- #include <QString>
- #include <QHash>
- #include "DataItem.h"
- class DataController
- {
- public:
- //DataController(void);
- ~DataController(void);
- //void addNew(DataItem * tPI);
- private:
- };
MyDockWidget.cpp
- #include "MyDockWidget.h"
- #include "DataController.h"
- #include <QString>
- MyDockWidget::MyDockWidget( )
- {
- //DataController myDataController;
- }
- int MyDockWidget::main(int p_argsc, char *p_argsv[] )
- {
- //this->myDataController = new DataController();
- myDataController.addNew(2511, "Heim", 20481, 1272923);
- myDataController.addNew(2512, "Work", 20482, 1272963);
- // Breakpoint here
- return 1;
- }
MyDockWidget.h
- #ifndef MY_DOCK_WIDGET_H
- #define My_DOCK_WIDGET_H
- #include <QString>
- #include "DataController.h"
- class MyDockWidget
- {
- //Q_OBJECT
- public:
- /// @brief constructor
- MyDockWidget();
- DataController myDataController;
- int main( int p_argsc, char *p_argsv[] );
- };
- #endif // MY_DOCK_WIDGET_H
I now get a Linker-Error:
1>Linking…
1>DataItem.obj : error LNK2019: unresolved external symbol “__declspec(dllimport) public: __thiscall QString::~QString(void)” (__imp_??1QString@@QAE@XZ) referenced in function __unwindfunclet$??0DataItem@@QAE@XZ$0
but I did not use any Libs or Dlls??! How is that be?
You must log in to post a reply. Not a member yet? Register here!






