Question

I am new to Qt and have started with the demo program found here. I have renamed the class AddressBook to Dialog. It compiles and executes just fine. I want to add my own code to the example, so I add a new label as a private member of the class:

private:
QCheckBox *native;
QLabel *integerLabel;
QLabel *doubleLabel;
QLabel *itemLabel;
...
QLabel *questionLabel;
QLabel *warningLabel;
QLabel *errorLabel;
QLabel *newLabel;     // <== This is new
QErrorMessage *errorMessageDialog;

In the constructor Dialog::Dialog I (try to) allocate memory to the variables:

warningLabel = new QLabel;
warningLabel->setFrameStyle(frameStyle);
QPushButton *warningButton = new QPushButton(tr("QMessageBox::&warning()"));

errorLabel = new QLabel;
errorLabel->setFrameStyle(frameStyle);
QPushButton *errorButton =
new QPushButton(tr("QErrorMessage::showM&essage()"));

newLabel = new QLabel;    // <== Error
newLabel->setFrameStyle(frameStyle);

As far as I can tell, I have created and allocated the variable correctly in this class, by copy and pasting *errorLabel and changing its name. Yet the compiler issues the error:

error: 'newLabel' was not declared in this scope

When I click the error, it takes me to the "new" line in the constructor. If I remark out the two lines in the constructor referring to newLabel, the program compiles. It IS in this scope in my mind. What mistake am I making here? How do we prevent these type issues in the future? (I have searched tens of similar posts and was able to identify the OP's mistake in 30s or less, but I cannot find my own, nor can I find a post where variables are declared in the class, but not found in the constructor.)

Thanks in advance, Kyle

Was it helpful?

Solution

I have solved the problem. Even though it is not the answer I expected, I post this answer just in case it is helpful to the rest of the community.

I created a new project and copied the cpp/h files from the non-working project into the new project. The new project compiles. With the new project I can now add other private variables to the class and access them in the constructor (as well as the IDE). All parameters I have easy access to show the projects to be identical, yet one compiles and the other does not. I can only conclude the project itself somehow became corrupted (which does not inspire confidence).

While I am relieved it was not a stupid programmatic mistake on my part, I am disappointed in not being able to find and fix whatever was wrong with the project files. Thanks to all those who offered suggestions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top