PDA

View Full Version : qstring.h no such file or directory error!



wenn32
1st May 2011, 11:53
hello i am trying to get this program work but it returns me error

QString.h no such file or directory error!

here is the code


#ifndef PUSHBUTTON_H
#define PUSHBUTTON_H
#include <QPushButton>
#include <QString> //here is where its showing error

class PushButton: public QPushButton
{
Q_OBJECT
public:
PushButton(QWidget* parent = 0): QPushButton(parent){}
PushButton(const QString& text, QWidget* parent = 0) : QPushButton(text, parent){}
public slots:
void updatenewvalues()
{
if(this->text() == "Don't click me!")
{
setText("Click me!");
return;
}
setText("Don't click me!");
}
};
#endif // PUSHBUTTON_H




int main(int argc, char* argv[])
{
QApplication app(argc, argv);
PushButton clickme("Click me!");
clickme.resize(200, 30);
clickme.setFont(QFont("Times", 18, QFont::Bold));
QObject::connect(&clickme, SIGNAL(clicked()), &clickme, SLOT(updatenewvalues()));
clickme.show();
return app.exec();
}


Thanks!

MasterBLB
1st May 2011, 12:56
Hmm everything seems OK,moreover this #include QString isn't necessary.
What is not fully right done is the connection of the signal.Move it into constructor of your class.

Show us compilation output please.

wenn32
1st May 2011, 15:42
connection of the signal???due to mean like this



PushButton::PushButton()
{
QObject::connect(&clickme, SIGNAL(clicked()), &clickme, SLOT(updatenewvalues()));
}


i am new to c++ but i can write few codes since i already have experience with c

ChrisW67
2nd May 2011, 03:45
Sort of, use "this" in place of "&clickme" (which will not compile anyway). You have two possible constructors (neither is parameterless) so you need to be sure that the connection gets made regardless of which is called (or eliminate one if you will never use it).

wenn32
4th May 2011, 10:38
ok here is it i have rewritten everything from the beginning



#ifndef PUSHBUTTON_H
#define PUSHBUTTON_H
#include <QPushButton.h>
class pushbutton : class QPushButton
{
Q_OBJECT
pushbutton(const QString& text, QWidget* parent = 0) : QPushButton(text, parent)
{
QObject::connect(this,signal(clicked()),this,slot( change()));
}

public slots:
void change()
{
if(this->text() == "Don't click me!")
{
setText("Click me!");
return;
}
setText("Don't click me!");
}
}
#endif




#include <QApplication>
#include "pushbutton.h"


int main(int argc, char* argv[])
{
QApplication app(argc, argv);
pushbutton button = new pushbutton("Dont click me");

button.show();
return app.exec();
}



these are the following errors that shows me


C:\Documents and Settings\t\My Documents\project\frontend\pushbutton.h|6|error: expected class-name before 'class'|

C:\Documents and Settings\t\My Documents\project\frontend\pushbutton.h|6|error: expected '{' before 'class'|

C:\Documents and Settings\t\My Documents\project\frontend\pushbutton.h|6|error: redefinition of 'class QPushButton'|

C:\Qt\2010.03\qt\include\QtGui\..\..\src\gui\widge ts\qpushbutton.h|58|error: previous definition of 'class QPushButton'|

C:\Documents and Settings\t\My Documents\project\frontend\pushbutton.h|6|error: new types may not be defined in a return type|

C:\Documents and Settings\t\My Documents\project\frontend\pushbutton.h|6|note: (perhaps a semicolon is missing after the definition of '<type error>')|

C:\Documents and Settings\t\My Documents\project\frontend\main.cpp|5|error: two or more data types in declaration of 'main'|

||=== Build finished: 6 errors, 0 warnings ===|



please help me Thanks!

stampede
4th May 2011, 11:13
You forgot the semicolon after class declaration.
edit: and your class' constructor is private now, add public: before definition of constructor

wenn32
4th May 2011, 11:22
ok thanks for the reply i have added the ';' after class declaration and public: to the constructor but still the problems occur



C:\Documents and Settings\t\My Documents\project\frontend\pushbutton.h|6|error: expected class-name before 'class'|

C:\Documents and Settings\t\My Documents\project\frontend\pushbutton.h|6|error: expected '{' before 'class'|

C:\Documents and Settings\t\My Documents\project\frontend\pushbutton.h|6|error: redefinition of 'class QPushButton'|

C:\Qt\2010.03\qt\include\QtGui\..\..\src\gui\widge ts\qpushbutton.h|58|error: previous definition of 'class QPushButton'|

C:\Documents and Settings\t\My Documents\project\frontend\pushbutton.h|26|error: multiple types in one declaration|

C:\Documents and Settings\t\My Documents\project\frontend\main.cpp||In function 'int main(int, char**)':|

C:\Documents and Settings\t\My Documents\project\frontend\main.cpp|8|error: variable 'pushbutton button' has initializer but incomplete type|

C:\Documents and Settings\t\My Documents\project\frontend\main.cpp|8|error: invalid use of incomplete type 'class pushbutton'|

C:\Documents and Settings\t\My Documents\project\frontend\pushbutton.h|6|error: forward declaration of 'class pushbutton'|

||=== Build finished: 8 errors, 0 warnings ===|

stampede
4th May 2011, 11:28
Inheritance requires the public, protected or private keywords, not class

class pushbutton : public /*private or protected, not class*/ QPushButton
{
Q_OBJECT
pushbutton(const QString& text, QWidget* parent = 0) : QPushButton(text, parent)
{
QObject::connect(this,signal(clicked()),this,slot( change()));
}
...
Better start with good C++ book before programming with Qt.

wenn32
4th May 2011, 15:01
its showing error in this code


pushbutton(const QString& text, QWidget* parent = 0) : QPushButton(text, parent)
{
QObject::connect(this,SIGNAL(clicked()),this,SLOT( change()));
}


its showing error in this part


obj\Debug\main.o||In function `pushbutton':|

C:\Documents and Settings\t\My Documents\project\frontend\pushbutton.h|11|undefin ed reference to `vtable for pushbutton'|

C:\Documents and Settings\t\My Documents\project\frontend\pushbutton.h|11|undefin ed reference to `vtable for pushbutton'|
||=== Build finished: 2 errors, 0 warnings ===|



i know that its basic c++ but i am more into C if i get this signal and slot part correct then i will be rolling :-)

stampede
4th May 2011, 15:07
Is the class header on the list of HEADERS in .pro file ? Check if you have moc_pushbutton.cpp somewhere in the build directories. I think the header is not moc'ed, so you get undefined references, make sure its in the list of HEADERS and run qmake/make again.
Btw. you don't have to use QObject:: for connections, pushbutton class is derived from QObject, so you can simply call connect(...).

tbscope
4th May 2011, 15:16
pushbutton button = new pushbutton("Dont click me");

Better start with learning C++ first. It is an absolute must for using Qt.

It is either:

pushbutton *button = new pushbutton("...");
or

pushbutton button("...");

wenn32
4th May 2011, 16:51
Is the class header on the list of HEADERS in .pro file ? Check if you have moc_pushbutton.cpp somewhere in the build directories. I think the header is not moc'ed, so you get undefined references, make sure its in the list of HEADERS and run qmake/make again.
Btw. you don't have to use QObject:: for connections, pushbutton class is derived from QObject, so you can simply call connect(...).

i am using code blocks IDE and i didn't find any .pro file in the project folder.so should i create a .pro file to get rid of the error??

wenn32
5th May 2011, 12:35
here is the main.cpp



#include <QApplication>
#include "pushbutton.h"

int main(int argc, char* argv[])
{
QApplication app(argc, argv);
pushbutton *button = new pushbutton("Dont click me");

button->show();
return app.exec();
}


here is the pushbutton.h



#ifndef PUSHBUTTON_H_
#define PUSHBUTTON_H_

#include <QPushButton>

class pushbutton : public QPushButton
{
Q_OBJECT

public:
pushbutton(const QString& text, QWidget* parent = 0) : QPushButton(text, parent)
{
connect(this,SIGNAL(clicked()),this,SLOT(change()) );
}

public slots:
void change()
{
if(this->text() == "Don't click me!")
{
setText("Click me!");
return;
}
setText("Don't click me!");
}
};
#endif


ok i did some research and this is what i did

first i runned the qmake -project with folder source as my project folder like

c:/document......../myproject>qmake -project

then i got .pro file which has following data


##Automatically generated by qmake (2.01a) Thu May 5 16:40:27 2011#####

TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

# Input
HEADERS += pushbutton.h
SOURCES += main.cpp



but still after doing that i am getting error as


obj\Debug\main.o||In function `pushbutton':|
C:\Documents and Settings\t\My Documents\project\sample\pushbutton.h|11|undefined reference to `vtable for pushbutton'|
C:\Documents and Settings\t\My Documents\project\sample\pushbutton.h|11|undefined reference to `vtable for pushbutton'|
||=== Build finished: 2 errors, 0 warnings ===|



i am using code blocks IDE.
Now can you guys help me.where am i doing wrong?????

i also did qmake -makefile and got few makefiles even that is not solving the problem.

stampede
5th May 2011, 13:55
I've never used codeblocks, maybe its generating it's own Makefiles. Leave the IDE build system for now, good that you try to use the tools by hand - this way you can learn a lot more than by clicking on the IDE gui buttons.
Try to cleanup all Makefiles in your project directories, run qmake --project again, then qmake and make. Code looks ok, so I guess this is caused by some leftovers in build directories.

wenn32
5th May 2011, 15:59
Finally it works! if you just search all the threads i have started.you will see that all my threads where on signals and slots and now it works can't believe it!!!!!

anyways i am really feeling bad that i couldn't make it work with code blocks(it would have been easier)

anyways i have written a program that will do QMAKE -project,QMAKE & MAKE(when placed in the project file)(that's less burden!!!)

so thanks to all the people who helped me THANKS!