Cannot open include file " but I can create objects "
-
wrote on 7 Mar 2014, 21:17 last edited by
Hey guys I have a weird problem here , I can't include my header :
error: C1083: Cannot open include file: 'calculator.h': No such file or directory
this is the code from last night I was just trying to use it as a class " still unfinished " but I think even though it's unfinished it should run just fine , but it doesn't.
main:
@
#include <QApplication>
#include<calculator.h>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
calculator *dialog;
dialog->show();return a.exec();
}
@
header:
@
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include<QLabel>
#include<QHBoxLayout>
#include<QLineEdit>
#include<QIntValidator>
class calculator : QWidget
{public:
calculator();void convert(); void convert1();
private:
QLineEdit *edit;
QLineEdit *edit1;
QIntValidator *Valid;
QHBoxLayout *left;
QHBoxLayout *Main;};
#endif // CALCULATOR_H
@implement:
@
#include<calculator.h>
calculator::calculator()
{
edit = new QLineEdit;
edit1=new QLineEdit;left= new QHBoxLayout; Main=new QHBoxLayout; edit->setMaximumWidth(20); edit->setFixedWidth(20); edit->setValidator(Valid); edit1->setMaximumWidth(20); edit1->setFixedWidth(20); edit1->setValidator(Valid); left->addWidget(edit); left->addWidget(edit1); Main->addLayout(left); setLayout(Main); QObject:: connect(edit,SIGNAL(editingFinished()),this,SLOT(convert)); QObject:: connect(edit1,SIGNAL(editingFinished()),this,SLOT(convert1));
}
void calculator::convert(){
QString numbertext=edit->text();
int x=numbertext.toInt();}
void calculator::convert1(){
QString numbertext=edit1->text();
int y=numbertext.toInt();}
@
project:
@
#-------------------------------------------------Project created by QtCreator 2014-03-07T23:16:20
#-------------------------------------------------
QT += core gui
QT += widgetsgreaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled14
TEMPLATE = appSOURCES += main.cpp
calculator.cppHEADERS +=
calculator.hFORMS += mainwindow.ui
@ -
wrote on 7 Mar 2014, 22:20 last edited by
I can't seems to fix it , I deleted everything in the class to see " maybe the error is in the coding " but it still gives the same problem.
I'll try to create a new class and see if it works.
-
wrote on 7 Mar 2014, 22:33 last edited by
it works now , but when I use :
calculator *p=new calculator;
and i use show function it doesn't show ?
-
Hi,
You are missing the base class initialization:
@
calculator::calculator(QWidget parent):
QWidget(parent)
{
//rest of the code
}
@Also you currently have a memory leak in your application since you don't delete dialog once the application is finished.
-
wrote on 7 Mar 2014, 22:48 last edited by
Hey , thanks for your help in advance.
I have a problem now when I compile it doesn't show the window then says
The program has unexpectedly finished.
--
and I didn't understand your code what is :
@
QWidget{
//code ?
}you mean I should put what I put in the constructor inside that QWIDGET braces ?
about the leak , lets just hope it runs first :D
-
wrote on 7 Mar 2014, 22:53 last edited by
oh yeah forgot about qwidget in the implement :D
-
wrote on 7 Mar 2014, 22:55 last edited by
Still same problem :(
-
wrote on 7 Mar 2014, 22:57 last edited by
the code " I changed the project to solve the first problem so I named the class to another name "
imp:
@
#include "checkclass.h"checkclass::checkclass(QWidget parent):
QWidget(){
edit = new QLineEdit;
edit1=new QLineEdit;left= new QHBoxLayout; Main=new QHBoxLayout; edit->setMaximumWidth(20); edit->setFixedWidth(20); edit->setValidator(Valid); edit1->setMaximumWidth(20); edit1->setFixedWidth(20); edit1->setValidator(Valid); QObject:: connect(edit,SIGNAL(editingFinished()),this,SLOT(convert)); QObject:: connect(edit1,SIGNAL(editingFinished()),this,SLOT(convert1)); left->addWidget(edit); left->addWidget(edit1); Main->addLayout(left); setLayout(Main);
}
void checkclass::convert(){
QString numbertext=edit->text();
int x=numbertext.toInt();}
void checkclass::convert1(){
QString numbertext=edit1->text();
int y=numbertext.toInt();}
@
header:
@
#ifndef CHECKCLASS_H
#define CHECKCLASS_H
#include<QLabel>
#include<QHBoxLayout>
#include<QLineEdit>
#include<QIntValidator>
#include<QWidget>
class checkclass : public QWidget
{
public:
checkclass(QWidget parent);void convert(); void convert1(); private: QLineEdit *edit; QLineEdit *edit1; QIntValidator *Valid; QHBoxLayout *left; QHBoxLayout *Main;
};
#endif // CHECKCLASS_H
@ -
Valid is not initialized
-
wrote on 7 Mar 2014, 23:01 last edited by
same problem :(
-
wrote on 7 Mar 2014, 23:22 last edited by
@SGaist , Hopefully you didn't go to sleep yet , I really need your help , because tomorrow I want to focus on studying for my mid term exam :D
-
wrote on 7 Mar 2014, 23:44 last edited by
The problem happens only when I use show function like for example:
myclass *A;
A->show();it crashes , when I don't use show() my program runs just fine in the backround.
-
[quote author="Project try" date="1394235877"]The problem happens only when I use show function like for example:
myclass *A;
A->show();it crashes , when I don't use show() my program runs just fine in the backround.[/quote]Like SGaist said, you didn't initialize your pointer. A does not point to a valid object yet, so it will cause problems if you try to use it. It's not just show(), ANY function can cause a crash -- it might not crash immediately, but will crash some time later.
Call new to initialize your pointer. This is a very fundamental (and very important) concept of C++ by the way; if you are not familiar with it, I highly recommend reading a tutorial about pointers.
-
wrote on 8 Mar 2014, 00:32 last edited by
Thanks man ^^ , it worked
but can you please tell me why did we put : QWidget *parent in the arguments of the constructor ? even though we didn't send any argument but it was a default so what's the point ?
and since it didn't take any argument there's no need to delete that parent pointer ? , because I saw a lot of examples and most of them use it like I said but they didn't really explain why.
-
wrote on 8 Mar 2014, 00:33 last edited by
Good night , I need to sleep , thanks guys for helping me , and sorry for asking lots of question :(
-
So that you can reuse your widget the same way you would a standard Qt widget.
Even if you don't reuse it somewhere else, it's good practice to keep a consistent code base. You never know who will use your code later
-
Parent-child relationships have multiple uses in Qt. One of them is memory management. When a parent object is deleted, it will automatically delete its child objects. Thus, you should never store or delete the parent pointer.
[quote]
@
checkclass::checkclass(QWidget parent):
QWidget(){
...
@
[/quote]You have not made use of the parent pointer. Pass it to the QWidget constructor, and let Qt take care of things in the background:@
checkclass::checkclass(QWidget parent):
QWidget(parent){
...
@
7/17