Hello. There was a problem with iterators when creating a vector with elements of the type of its class.

int main(){ QVector<myClass> vector; myClass x; for (int i = 0; i < 5; i++) { xx=i; xy=i; vector.push_back(x); } QVector<myClass>::iterator iter=vector.begin(); for (;iter!=vector.end();++iter) { qDebug()<<*iter.x; } } 

The class itself:

 class myClass { public: myClass(); int x,y; }; 

I tried to set the type of the element of the vector int , everything works. In the documentation I found examples only with standard classes.

  • In this case, the class is redundant. Use struct :-) - gecube
  • Hello, I wrote for an example) the essence of the problem is different, I do not have to work with a vector with the help of an iterator, the elements of which are class objects. I hope I put the thought right. - mazide

2 answers 2

Good day! Himself toiled with a similar problem. In order to use iterators, for its class, as well as to use it in various typable structures like List <>, you need to define for your class some of the following constructs: default constructor, shocking constructor, comparison operator =, and also the function of obtaining white from your class. Exactly which ones are necessary - it is difficult to say for each of the typed classes this set is its own, but they are needed, if you have questions about what it is, I can dig and send you an example of my class working with such types. Here is my example, which I, at one time, took from the Qt documentation, partially added and used as the basis in subsequent classes:


 #ifndef MESSAGE_H #define MESSAGE_H #include <QHash> #include <QString> #include <QStringList> class Message { public: Message(); Message(const Message &other); ~Message(); Message(const QString &body, const QStringList &headers); QString body() const; QStringList headers() const; friend bool operator==(const Message& left, const Message& right); friend uint qHash(const Message& message); private: QString m_body; QStringList m_headers; }; #endif // MESSAGE_H 


 #include "message.h" Message::Message() { } Message::Message(const Message &other) { m_body = other.body(); m_headers = other.headers(); } Message::~Message() { } Message::Message(const QString &body, const QStringList &headers) { m_body = body; m_headers = headers; } QString Message::body() const { return m_body; } QStringList Message::headers() const { return m_headers; } bool operator==(const Message& left, const Message& right) { return !left.body().compare(right.body()); } uint qHash(const Message& message) { return ::qHash(message.body()); } 
  • In fact, it is possible that some of these structures do not need to be determined, since the compiler will make them by default. Link to documentation: - gecube
  • [Reference to documentation:] [1] To qualify, a type must be a default constructor, a copy constructor, and an assignment operator. This is a cover of the QObject or any other types of data types, such as QTring, QDate, and QTime. QObject subclass (QWidget, QDialog, QTimer, etc.). [1]: doc.qt.nokia.com/4.7-snapshot/containers.html - gecube
  • If you’re trying to instantiate a QList <QWidget>, the compiler will complain that QWidget’s copyright constructor and assignment rules are disabled. QList <QWidget *> if you want to store these types of objects. - gecube
  • MaSTeR_Alexandr, if it is not difficult for you, it would be very cool) - mazide am

You dereference int, but not the iterator. That's right: iter-> x or (* iter) .x

  • And the iterator knows nothing about x (iter.x cannot be written) - Baho