Qt World Summit: Submit your Presentation

Error in this program

  • 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) :
    ui(new Ui::MainWindow)

    delete ui;


    void MainWindow::on_pushButton_clicked()

    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;
    bool found = false;
    ifstream my;
    QMessageBox msg;
    msg.setText("Company Not Found!");
    void MainWindow::on_actionExit_2_triggered()
    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)?

  • Moderators

    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

  • Moderators

    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.

  • 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?

  • Lifetime Qt Champion


    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.

  • Moderators

    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.

Log in to reply