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. Error in this program
Forum Updated to NodeBB v4.3 + New Features

Error in this program

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 1.6k Views 1 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.
  • R Offline
    R Offline
    Raminlich
    wrote on last edited by
    #1

    Hi All
    im new to Qt here is my Error:

    E:\Qt\Projects\Calcu\mainwindow.cpp:38: error: no match for 'operator<<' (operand types are 'QTextStream' and '<unresolved overloaded function type>')
    input<<name<<" "<<amount<<" "<<d<<"/"<<m<<"/"<<y;

    and this is my Program
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "fstream"
    #include "QFile"
    #include "QTextStream"
    #include "QMessageBox"
    using namespace std;

    QString name;
    int amount;
    int d,m,y;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    ui->groupBox_3->hide();
    }

    MainWindow::~MainWindow()
    {
    delete ui;

    }

    void MainWindow::on_pushButton_clicked()
    {

    name=ui->lineEdit_name->text();
    amount=ui->lineEdit_amount->text().toInt();
    d=ui->lineEdit_d->text().toInt();
    m=ui->lineEdit_m->text().toInt();
    y=ui->lineEdit_y->text().toInt();
    ofstream my("e://mystock.txt");
    QFile mys("e://mystock.txt");
    if(mys.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
        {
    QTextStream input(&mys);
    input<<name<<" "<<amount<<"    "<<d<<"/"<<m<<"/"<<y;
    

    }
    }

    void MainWindow::on_pushButton_2_clicked()
    {
    QString srch;
    srch=ui->lineEdit_srch->text();
    bool found = false;
    ifstream my;
    while(my>>name>>amount){
    if(srch==name){
    ui->groupBox_3->show();
    found=true;
    }
    if(!found){
    QMessageBox msg;
    msg.setText("Company Not Found!");
    msg.exec();
    }
    }
    }
    void MainWindow::on_actionExit_2_triggered()
    {
    QApplication::exit();
    }@
    as you can see i Select the variables as Global Because I want use this variables All Over Of my Program
    And I know thats the cause of my Error What must i do to fix this(I Want To use Global Variables Without Error)?
    Thanks.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi, welcome to devnet,

      It doesn't matter if these are global or not, although you really should make them MainWindow members at least or just make a getter member functions. I mean a global variable named "d"? That begs for trouble.

      Is that the complete code? Do you have a function called "name" or "amount" somewhere? Do you use templates or function pointers? this error message indicates compiler is confused about some types.

      Some unrelated comments:

      • You might want to look into difference between #include "..." and #include <...>.
      • "e://mystock.txt" - what's the double slash for? Use "e:/mystock.txt" or "e:\mystock.txt"
      • in UI applications instead of calling QApplication::exit() call close(). This will close the window, cleanup and exit the app. QApplication::exit is like killing the process. Not very nice way to finish and won't clean up any open handles.
      • @ifstream my;
        while(my>>name>>amount)@ This can't be right
      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Oh yeah, I just remembered. QMainWindow actually has a member function called y() and that's the problem. Compiler thinks you want to print a member function, and that's nonsense.

        That's what you get for creating one-letter global variables :) Name your variables something meaningful, like "year" and stay away from globals.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          blue_sky
          wrote on last edited by
          #4

          Yes the problem is - it takes the member function by default and no artificial intelligence to detect which variable we really need and gives you error. For accessing the global, :: to use. i.e. ::y will resolve the issue.
          So,Please help me for the below behavior -
          Same name to
          1 - memberFunction and Constructor,
          2 - memberFunction and memberVariable

          why (1) is error, if we call constructor only after class name and memberFunction after object or pointer or scope resolution operator?

          and what is problem with (2) so that error?
          Thanks....

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

            Hi,

            Like Chris Kawa wrote, it's bad practice.

            1 You can't have a memberFunction with the same name as a Constructor, it's just another constructor.

            2 You can't use the same name for a member variable and a function. It's easy to simply prefix your member variable.

            That's all basic C++, please grab a good book about the subject.

            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
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Yup, it's just the syntax of c++.
              @
              class Foo {
              Foo(float param); //this is suppose to be a constructor
              Foo(double param); //this is suppose to be a member
              };
              @ How is compiler suppose to know what you want???
              These are both constructors. Period.
              @
              class Foo {
              void bar(int);
              static int bar;
              };
              auto x = Foo::bar; // x is int or void(Foo::*)(int)???
              @
              The compiler can't know. It doesn't have mind reading powers and even if it had, you might want it one way and the next programmer another way.

              Imagine if all the methods in Qt were named like that: a(), b(), f(), um(), duh()... It would be a pain to use and you would have to dig through a ton of documentation/code to find what you need.
              Just name your variables right - short but descriptive. One look should be enough to know what it does. And use notation standards like CamelCase or m_Hungarian. You will make life a lot easier for yourself and others who will read your code.

              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