Hello forum users! Describing the situation: the AddressBookParser class does not see the TextEdit class. Both are derived from the corresponding QT library classes. * Errors *:

'TextEdit' doesn’t name a type TextEdit * edit;

AND

addressBookParser :: AddressBookParser (TextEdit *, QString) 'AddressBookParser * parcer = new AddressBookParser (this, edit-> text ());

Ie everywhere where I address or create an object of type TextEdit

I tried everything I know, starting from the TextEdit prototype before declaring AddressBookParcer (I read somewhere that if files are connected to each other in the list 1-> 2-> 3, then 3 can recognize the 2nd class, xs as it is called), ending with the addition strictly correct constructors of type TextEdit (QWidget * p): QTextEdit (p) {}.

Why does the compiler say that there is no TextEdit? And how to fix it? I appeal to your wisdom, THANKS! The PS program looks at the XML document, and prints the contact by number ("number =" i "), which is entered in QLineEdit and should output the contents of this contact to TextEdit, and yes, SAX is used here (See M. Posting Qt5.3 Chapter 40)

//addressbookparcer.h #ifndef ADDRESSBOOKPARSER_H #define ADDRESSBOOKPARSER_H #include <QXmlDefaultHandler> #include <QDebug> #include <QtWidgets> #include <QException> #include <QMessageBox> #include "textedit.h" class AddressBookParser : public QXmlDefaultHandler { private: TextEdit* edit; QString _text; // данные int number; bool finded; public: AddressBookParser(TextEdit* e, QString num) { number = num.toInt(); edit = e; finded = false; } //переопределенные методы, расположены в порядке вызова virtual bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts) //атрибуты документа { finded = false; for(int i = 0; i < atts.count(); i++){ if(atts.value(i) == QString::number(number)) { edit->setText(edit->toPlainText() + "\nAttribute: " + atts.value(i)); finded = true; } } return true; } virtual bool characters(const QString& text) { _text = text; return true; } virtual bool endElement(const QString&, const QString&, const QString& str) { if(str != "contact" && str != "addressbook" && finded) { //не обрабатываем /contact, /addressbook edit->setText(edit->toPlainText() + "\nTag name: " + str + "\t Text: " + _text); } return true; } virtual bool fatalError(const QXmlParseException& ex) //обработка исключительных ситуаций { qDebug() << "Line: " << ex.lineNumber() << "\t Column: " << ex.columnNumber() << "\t Message: " << ex.message(); return false; } }; #endif // ADDRESSBOOKPARSER_H //textedit.h #ifndef TEXTEDIT_H #define TEXTEDIT_H #include "addressbookparser.h" class TextEdit : public QTextEdit { Q_OBJECT private: QLineEdit* edit; public: TextEdit(QLineEdit* edit){ this->edit = edit; } public slots: void parcerSlot(bool) { bool f; edit->text().toInt(&f); if(!f) return; AddressBookParser* parcer = new AddressBookParser(this,edit->text()); QFile* file = new QFile("addressbook.xml"); QXmlInputSource source(file); //объект источника для парсинга документа QXmlSimpleReader reader; //ридер документа reader.setContentHandler(parcer); //устанавливаем обработчик reader.parse(source); //запуск парсинга } }; #endif // TEXTEDIT_H //main.cpp #include "textedit.h" int main(int argc, char** argv) { try{ QApplication app(argc, argv); QWidget wgt; wgt.resize(600,100); QVBoxLayout* layout = new QVBoxLayout(&wgt); QLineEdit* line = new QLineEdit; TextEdit* edit = new TextEdit(line); QPushButton* button = new QPushButton("обработать"); layout->addWidget(line); layout->addWidget(edit); layout->addWidget(button); wgt.show(); QObject::connect(button, SIGNAL(clicked(bool)), edit, SLOT(parcerSlot(bool))); return app.exec(); } catch(QException& ex) //обработка ошибок { QMessageBox M; M.setText(QString(ex.what())); M.show(); } } 
  • Does the header textedit.h include a header with the AddressBookParser definition? - Vlad from Moscow
  • @VladfromMoscow, yes, but I tried to clean up and do other inclusions - Xambey

1 answer 1

You have a textedit.h header textedit.h includes the addressbookparser.h header.

 //textedit.h #ifndef TEXTEDIT_H #define TEXTEDIT_H #include "addressbookparser.h" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

Therefore, this first-included headline does not see a TextEdit declaration, as a result, the AddressBookParser class is declared before the TextEdit class declaration.

You should first declare classes, and then define its functions outside the class. And in the headlines, just include preliminary type declarations

 class AddressBookParser; 

Here is what the textedit.h header looks like, for example.

 // textedit.h #ifndef TEXTEDIT_H #define TEXTEDIT_H class AddressBookParser; 

// You can even remove this declaration if there is no link to it in the declaration of the TextEdit class

 class TextEdit : public QTextEdit { Q_OBJECT private: QLineEdit* edit; public: TextEdit(QLineEdit* edit){ this->edit = edit; } public slots: void parcerSlot(bool); }; #endif // TEXTEDIT_H 

And define the function itself in a separate file by including these two headers.

  • So what do you suggest? I kind of tried to do this: main.cpp (textedit.h), textedit.h (addressbookparcer.h), addressbook.h (nothing, but added class TextEdit before declaring) - Xambey
  • @Xambey W wrote what I suggest. - Vlad from Moscow
  • Well, right now, I'll try one more time - Xambey
  • Thanks, helped - Xambey