Link to home
Start Free TrialLog in
Avatar of ChLa
ChLaFlag for United States of America

asked on

I need help with an undeclared identifier error in C++ using QT Creator

Hello, I am creating my first program for Linux using C++ and QT Creator 4.11.0
I am new to QT Creator, and I am not extremely familiar with C++.I have been using Delphi to create programs for Windows since 1998.
At this point I have the basic framework for my program and it runs. I have a nice working UI, have learned signals and slots, created several slots, and have a button causing text to be written to a lineEdit.
I have one problem relating to an undeclared identifier error, that prevents the program from running, and also prevents me from making further progress on this program. There is a second related problem that apparently is due to a problem in the code QT Creator created for me. Also there is a problem with mainwindow.h that QT Creator also created for me.
I have included the code. This code includes MainWindow.h, Main.cpp and Mainwindow.cpp. The only thing I have removed is several empty slot functions that are identical to the ones you can see.

Regarding the first problem, the one that is preventing the program from running. Look at MainWindow.cpp. Everything here was written for me automatically by QT Creator except for the function RefreshDisplay(), my function that will display several variables in LineEdits, and the line in RefreshDisplay() and in MainWindow::on_pushButton_clicked(),
 that writes some text to a lineEdit. The other functions are slots created automatically for me. They will have some math added, and they call RefreshDisplay(); The first problem, is that the line that puts some text into the lineedit works from within the slot function created by QT Creator, but it doesn’t work from the function I wrote, RefreshDisplay(). Here it gets an undeclared identifier error for “ui”. I cannot figure out why this happens here in my function, and not in the slot functions. I think I need to declare ui for my function. But I don’t know where or how. I’ve tried several things that didn’t work. If someone can show me how to fix this, I will be able to move forward.

Regarding the second problem, for each of the slot functions created for me by QT Creator, there is an error message saying “Use of undeclared identifier ‘MainWindow’ ”. This is not preventing the program from running, but I would like to get rid of all these error messages if I can. I have no idea how to fix this, other than that MainWindow is not being properly declared, possibly in the header.

Finally, the third problem. There seems to be something wrong in the file MainWindow.h, which was automatically created for me by QT Creator. The cause of this problem may also be the cause of the second problem.
The first symptom of this problem is a message at the top of the MainWindow.cpp editing window in yellow that says: “Warning: This code model could not parse an included file, which might lead to incorrect code completion and highlighting, for example.”
The second symptom is the identical message, also in a yellow window at top of the MainWindow.h editor.
Finally, the third symptom: In MainWindow.h, which was written automatically by QT Creator, where it defines class MainWindow. That line has an error message saying: “Expected class name”. The next line just below { and above the public declaration, says QOBJECT. This line has four error messages. They are:
“Incomplete result type ‘QString’ in function definition”, “Calling ‘TR’ with incomplete return type ‘QString’.”, “Incomplete result type ‘QString” in function definition” and “Calling ‘TR’ with_”.

I don’t know how to fix this either. Perhaps the word QOBJECT needs a semicolon at the end.

I appreciate any help you can give me on this. Here is the code:

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private slots:
    void on_pushButton_2_clicked();
    void on_pushButton_clicked();
    void on_pushButton_3_clicked();
    void on_pushButton_4_clicked();
    void on_pushButton_13_clicked();
    void on_pushButton_14_clicked();
    void on_pushButton_11_clicked();
    void on_pushButton_12_clicked();
    void on_pushButton_5_clicked();
    void on_pushButton_6_clicked();
    void on_pushButton_7_clicked();
    void on_pushButton_8_clicked();
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

Main.cpp

#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}
MainWindow::~MainWindow()
{
    delete ui;
}
void RefreshDisplay()                           //Updates Displayed Variables
{
    //ui->lineEdit->setText("Boogeyman");         //this line causes compile                      //failure because of undeclared identifier "ui"
}
void MainWindow::on_pushButton_2_clicked()      //Total Frames Increase Button Slot
{
    RefreshDisplay();
}
void MainWindow::on_pushButton_clicked()        //Total Frames Decrease Button Slot
{
    ui->lineEdit->setText("Boogeyman");         //Prints to lineEdit successfully
}


Avatar of ChLa
ChLa
Flag of United States of America image

ASKER

I found that I could click on Show Details for the yellow bar at the top. The new info seems to indicate that I need to include stddef.h. I tried adding the include statement to all three files, one at a time. In every case it says file not found. I used Tools>locate and found it. I double-clicked on it and it opened. But it is not part of the project. I don't know if I should copy it to some other location, or if I need to add the path to somewhere in project options.
Avatar of Fabrice Lambert
Concerning your first issue, the ui pointer is a private member of the MainWindow class, wich mean only the class and its function members can access it.
Alas, the RefreshDisplay function is not a member of the MainWindow class.
So define and implement the RefreshDisplay function as member of the class and the compiler won't complain anymore.

Side note: As suggested in your previous question, you should study C++ and classes before jumping on QT.
If you are looking for a great book, look for C++ Primer 5th edition, written by Stanley B. Lippman and Josée Lajoie 
(do not confuse with C++ Primer plus, this one sux).
Avatar of ChLa

ASKER

I tired adding "void RefreshDisplay();" to mainwindow.h inside the class MainWindow declaration, under public, after -MainWindow(); I built it. It didn't help, I still get the same undefined identifier error. I've been reading C++ books. I just ordered the one you suggested. Still, I learn well by writing a simple program. Finding the how to define and implement my function as a member of the class will help me learn. I understand the concept. I just don't know the syntax, or where to put it, or do I need to add to the function. I tried before to add MainWindow:: to the start of my function, but that didn't work. Perhaps they will work if I do both. But I am about to leave for the day. Thanks for being patient with me.
ASKER CERTIFIED SOLUTION
Avatar of Fabrice Lambert
Fabrice Lambert
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ChLa

ASKER

Thank you very much. That did it. I had tried both things, but not at the same time. And I didn't know the correct semantic for the class implementation. I was putting the word "void" after the "::". Now the program runs and I can move ahead.

Regarding the third problem, I was looking at the class definition. The first line with the word "Q_OBJECT" doesn't look right to me and I don't know what it is supposed to do. I tried hiding it (//) and a bunch of the issue messages went away. But then the program will not run.

I ordered that book because after getting a couple of basic introduction books I was looking for something better like a reference manual.
I can't say much about QT, all I can say is that lines starting with QT_ or Q_ are macro who's sole purpose is to ensure QT behave as expected, and should be left untouched.