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 Offline
    N Offline
    Nils A
    wrote on last edited by
    #1

    Hello, I'm using Qt Creator 3.5.1 (opensource), Based on Qt 5.5.1 (GCC 4.9.1 20140922, 64 bit), Built on Oct 13 2015 07:38:30

    I try the Notepad code from "Getting Started Programming with Qt Widgets" from Qt at link http://doc.qt.io/qt-5/gettingstartedqt.html

    In the fileopen dialogue is a part:
    QTextStream in(&file);
    ui->textEdit->setText(in.readAll());
    file.close();

    When I run this in the Qt ide (green arrow) I get the error:
    error: base operand of '->' has non-pointer type 'Ui::NoteWindow'
    ui->textEdit->setText(in.readAll());
    .... ^
    Since I'm new to Qt and especially Qt 5.5.1, I wonder what I do wrong? because I try to run an example I take from the Qt5 site itself.

    Here the code from the example on Qt site:
    http://doc.qt.io/qt-5/gettingstartedqt.html

    void Notepad::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();
    }
    

    }

     Thanks in advance, Nils A
    
    1 Reply Last reply
    1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      It means that it is not a pointer thus your notation is not correct. How did you declare it ?

      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
        #3

        Hello Thanks,

        Since I try the example from the site I did not declare the textEdit or the ui. This was done in / by the design mode of Qt5.

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          hi
          and you have the
          notepad.ui
          as part of the project?
          also
          in mainwindow.h
          Do you have
          ...
          private:
          Ui::Notepad *ui;

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

            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 1 Reply Last reply
            1
            • 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