Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. base operand of '->' has non-pointer type
Forum Updated to NodeBB v4.3 + New Features

base operand of '->' has non-pointer type

Scheduled Pinned Locked Moved Solved General and Desktop
base operand ofnon-pointer typ
15 Posts 3 Posters 11.6k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Nils A

    Yes I have absolutely. I follow all steps in the http://doc.qt.io/qt-5/gettingstartedqt.html link. That's why I'm a bit surprised with the error. I'm slow in reply because I can only reply every 500 seconds as a new user ... (As a new user, you can only post once every 500 second(s) until you have earned 2 reputation - please wait before posting again)

    Nils A.

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #6

    can u check if u type
    ui->
    if it will suggest anything?
    also, if u dblclick the UI it opens fine etc?
    Also,
    you do have
    Ui::Notepad *ui; ?

    N 1 Reply Last reply
    0
    • mrjjM mrjj

      can u check if u type
      ui->
      if it will suggest anything?
      also, if u dblclick the UI it opens fine etc?
      Also,
      you do have
      Ui::Notepad *ui; ?

      N Offline
      N Offline
      Nils A
      wrote on last edited by
      #7

      @mrjj Hello,

      When I type ui it will give suggestions only after I type ::
      If I choose ui::textEdit->setText(in.readAll()); I get an error,
      Noteapp/notewindow.cpp:30: error: 'ui' is not a class or namespace
      ui::textEdit->setText(in.readAll());
      ^

      But, because this is an example application fron Qt on their site, why would they make it with errors? Do they test these apps?

      Nils A.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        Nils A
        wrote on last edited by
        #8

        Here the files: noteapp.pro, notewindow.h and notewindow.ccp

        // noteapp.pro
        #-------------------------------------------------

        Project created by QtCreator 2015-12-27T20:58:35

        why is this so big??

        #-------------------------------------------------

        QT += core gui

        greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

        TARGET = Noteapp
        TEMPLATE = app

        SOURCES += main.cpp
        notewindow.cpp

        HEADERS += notewindow.h

        FORMS += notewindow.ui


        // notewindow.h
        #ifndef NOTEWINDOW_H
        #define NOTEWINDOW_H

        #include "ui_notewindow.h"

        class NoteWindow : public QMainWindow
        {
        Q_OBJECT

        public:
        explicit NoteWindow(QWidget *parent = 0);

        private slots:
        void on_quitButton_clicked();

        void on_actionOpen_triggered();

        void on_actionSave_triggered();

        private:
        Ui::NoteWindow ui;
        };

        #endif // NOTEWINDOW_H


        // notewindow.ccp

        #include "notewindow.h"
        #include <QFileDialog>
        #include <QFile>
        #include <QMessageBox>
        #include <QTextStream>

        NoteWindow::NoteWindow(QWidget *parent) :
        QMainWindow(parent)
        {
        ui.setupUi(this);
        }

        void NoteWindow::on_quitButton_clicked()
        {
        qApp->quit();
        }

        void NoteWindow::on_actionOpen_triggered()
        {
        QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString(),
        tr("Text Files (.txt);;C++ Files (.cpp *.h)"));

          if (!fileName.isEmpty()) {
              QFile file(fileName);
              if (!file.open(QIODevice::ReadOnly)) {
                  QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
                  return;
              }
              QTextStream in(&file);
              ui->textEdit->setText(in.readAll());
              file.close();
        
              //error: base operand of '->' has non-pointer type 'Ui::NoteWindow'
              //         ui->textEdit->setText(in.readAll());
              //             ^
          }
        

        }

        void NoteWindow::on_actionSave_triggered()
        {
        QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QString(),
        tr("Text Files (.txt);;C++ Files (.cpp *.h)"));

        if (!fileName.isEmpty()) {
            QFile file(fileName);
            if (!file.open(QIODevice::WriteOnly)) {
                // error message
            } else {
                QTextStream stream(&file);
                stream << ui->textEdit->toPlainText();
                stream.flush();
                file.close();
            }
        }
        

        }

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #9

          ui is not a pointer so you should use ui.textEdit->toPlainText()

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • N Offline
            N Offline
            Nils A
            wrote on last edited by
            #10

            Yes, that's it! You are great. Why would Qt make this mistake in their example code?

            thanks,
            Nils A.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #11

              There's no mistake in the example, please read again the Notepad class. In the header they are declaring a pointer for ui and the initialization also shows they are allocating a new instance of Ui::Notepad in the constructor member initializer list

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • N Offline
                N Offline
                Nils A
                wrote on last edited by
                #12

                Thanks again, but the example on the site really uses the -> and not a .

                QTextStream in(&file);
                ui->textEdit->setText(in.readAll());
                file.close();

                see http://doc.qt.io/qt-5/gettingstartedqt.html

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #13

                  Yes, and again the example is correct.

                  Your code is a bit different: you have ui as Ui::Notepad ui;. In the example it's Ui::Notepad *ui;

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  N 1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    Nils A
                    wrote on last edited by
                    #14

                    I see yes, but still strange (and an error) because the (in my case) NoteWindow class is created by the Qt-widget wizard. I did not type the code.

                    example code on the site;
                    #include <QMainWindow>

                    namespace Ui {
                    class Notepad;
                    }

                    class Notepad : public QMainWindow
                    {
                    Q_OBJECT

                    public:
                    explicit Notepad(QWidget *parent = 0);
                    ~Notepad();

                    private:
                    Ui::Notepad *ui;
                    };

                    ======
                    example created by the Qt-Wizard:

                    #ifndef NOTEWINDOW_H
                    #define NOTEWINDOW_H

                    #include "ui_notewindow.h"

                    class NoteWindow : public QMainWindow
                    {
                    Q_OBJECT

                    public:
                    explicit NoteWindow(QWidget *parent = 0);

                    private:
                    Ui::NoteWindow ui;
                    };

                    #endif // NOTEWINDOW_H

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Yes, and again the example is correct.

                      Your code is a bit different: you have ui as Ui::Notepad ui;. In the example it's Ui::Notepad *ui;

                      N Offline
                      N Offline
                      Nils A
                      wrote on last edited by Nils A
                      #15

                      @SGaist Thanks again Champ. You are right, there is no error in the example. The mixing of the example code (using the *) with the auto-generate code (not using the *) made my C++ virgin brain confused. But now I will never forget to check for *'s any more.
                      Have a good day! (now how to set this to solved?) (Ah, found this too. Solved!)

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved