November 25, 2011

shadowfox shadowfox
Lab Rat
8 posts

help! is there errors in codes copied from book ?

 

I’m a total greenhand on qt,and I lent some books form library. I copied the “helloworld” code from the book to my QTcreator, and it ran perfectly. But when I started to build some more conplex code copied from the book, it start to make errors.
this is the code:

  1. //addressbook.h
  2.  
  3. #ifndef ADDRESSBOOK_H
  4. #define ADDRESSBOOK_H
  5.  
  6. #include <QWidget>
  7.  
  8. class QLineEdit;
  9.  
  10. class AddressBook : public QWidget
  11. {
  12.     Q_OBJECT
  13. public:
  14.     AddressBook(QWidget *parent = 0);
  15. private:
  16.     QLineEditor * nameline;
  17.     QTextEditor * addresstext;
  18. };
  19.  
  20. #endif // ADDRESSBOOK_H

It said the “QLineEditor” (and lots of other things) need declaeration in the scope. Is there missing some lib or header?

Edit: please use @ tags around code sections; Andre

7 replies

November 25, 2011

Eddy Eddy
Area 51 Engineer
1296 posts

In the adressbook.cpp file you should also use:

#include <QLineEdit>

Google for Forward declaration to understand why.

Please add @ code tags on your code. See the button with <> on it on top of the editor here.

 Signature 

Qt Certified Specialist
Qt Ambassador

November 25, 2011

qxoz qxoz
Area 51 Engineer
604 posts

You must include QLineEdit and other which you use

  1. #include <QLineEdit>

Edit:
As Eddy says. I’m late little bit :)

November 25, 2011

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

It is spelled QLineEdit, not QLineEditor. In addition, you are missing a forward declaration for QTextEdit in your .h file.

November 25, 2011

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts
qxoz wrote:
You must include QLineEdit and other which you use #include <QLineEdit>

As long as you just use pointers a forward declaration is sufficient. There is no need to include headers.

November 25, 2011

Volker Volker
Robot Herder
5428 posts
Lukas Geyer wrote:
As long as you just use pointers a forward declaration is sufficient. There is no need to include headers.

In fact, in most cases it’s good practice to not include the header where a forward declaration is sufficient. Speeding up compile time is only one of the advantages.

November 25, 2011

shadowfox shadowfox
Lab Rat
8 posts
Lukas Geyer wrote:
It is spelled QLineEdit, not QLineEditor. In addition, you are missing a forward declaration for QTextEdit in your .h file.

I think that’s the point. the reason i include qlineedit is it said something is not decleared. there is no include qlineedit on the book.

November 25, 2011

Andre Andre
Area 51 Engineer
6031 posts

Samples in books often ommit includes for classes provided by well-known libraries for the sake of brevity. Often, you can download the samples as working source code, and there the needed includes will be there.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

 
  ‹‹ Error Message I get when starting up Qt project.      how to read the color of an rectitem ››

You must log in to post a reply. Not a member yet? Register here!