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) :
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. -
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
-
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 memberVariablewhy (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.... -
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.
-
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.