I am new to qt creator.
there is an error in gui.cpp
24: error: expected primary-expression before 's1'
n=string1::len(QString s1);
gui.cpp
#include "gui.h"
#include "ui_gui.h"
#include "string1.h"
gui::gui(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::gui) {
ui->setupUi(this); }
gui::~gui() {
delete ui; }
void gui::on_pushButton_clicked() {
QString q1;
string1 *s1;
int n;
q1=ui->lineEdit->text();
s1=new string1(q1);
n=string1::len(QString s1);
ui->lineEdit_2->setText(q1); }
gui.h
#ifndef GUI_H
#define GUI_H
#include <QMainWindow>
#include<string1.h> namespace Ui { class gui; }
class gui : public QMainWindow {
Q_OBJECT
public:
explicit gui(QWidget *parent = 0);
~gui();
private slots:
void on_pushButton_clicked();
private:
Ui::gui *ui; };
#endif // GUI_H
string1.cpp
#include<string1.h>
string1::string1(QString q1) {
q=q1; } string1::string1() {
q=" "; }
int string1::len(QString *s) {
int i=0;
while(s[i]!=0)
{
i++;
}
i--;
return i; }
string1.h
#ifndef STRING1_H
#define STRING1_H
#include<qstring.h>
#include<gui.h>
class string1 {
QString q;
public:
friend class gui;
string1 *s1;
string1();
string1(QString q1);
int len(QString *s);
};
#endif // STRING1_H
The error is caused by n=string1::len(QString s1);, which is declaration of QString s1 inside the argument list. You probably copied the function declaration from the string1.cpp, changed the parameter name to the argument name, but forgot to remove the type. But it still won't work as string:::len is not static.
Are sure you need s1 to be dynamically allocated? It's local variable and you forgot to delete before the function ends. If you're using dynamically allocated string1 just to pass it to string1::len avoiding copying, google C++ references.
Related
I am trying to access the member of a class declared in another class as an attribute.
The project class has a workArea that is also a class defined in the project.h.
I instanciate a project in the main, and then I send it to the mainWindow through its constructor. Once in the mainWindow constructor I try to access project->lat that works fine, but project->workArea->latInf crashes.
Any help will be highly appreciated.
project.h
#ifndef PROJECT_H
#define PROJECT_H
#include <QObject>
class WorkArea{
public://attributes
int latInf = 30;
public://methods
WorkArea()//Default constructor
{
}
~WorkArea();
};
class Project : public QObject
{
Q_OBJECT
public: //attributes
int lat = 20;
WorkArea* workArea;
public: //methods
explicit Project(QObject *parent = nullptr);
signals:
public slots:
};
#endif // PROJECT_H
main.cpp
#include "ui/mainwindow.h"
#include <QApplication>
#include "project.h"
int main(int argc, char *argv[])
{
Project* pj;
QApplication app(argc, argv);
MainWindow w( 0 , pj);
w.show();
return app.exec();
}
mainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent, Project *project) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
try
{
qDebug() << "Project latInf" << project->lat << "\n"; // works fine
qDebug()<< "Project lowLeft: " << project->workArea->latInf << "\n" ; // crashes
}
catch(std::exception &ex)
{
qDebug() << ex.what() ;
}
catch (...)
{
}
}
That was it. Thanks #eyllanesc. I forgot to create workArea. I though just instanciting it as an attribute in Project was fine, but I had to fill that memory space in the constructor of project.
project.h
#ifndef PROJECT_H
#define PROJECT_H
#include <QObject>
class WorkArea{
public://attributes
int latInf = 30;
public://methods
WorkArea()//Default constructor
{
}
~WorkArea();
};
class Project : public QObject
{
Q_OBJECT
public: //attributes
int lat = 20;
WorkArea* workArea;
public: //methods
explicit Project(QObject *parent = nullptr){
workArea = new WorkArea;
}
signals:
public slots:
};
#endif // PROJECT_H
I'm trying to make a fake FBI program for a prank, but it's not as easy as I expected it to be.
This is my code.
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->trackConnButton->setEnabled(false);
}
void MainWindow::on_confirmButton_clicked() {
if (ui->usernameText == "name" && ui->passwordText == "password") {
ui->resullabel->setText("Password accepted.");
ui->trackConnButton->setEnabled(true);
} else {
ui->resullabel->setText("Password denied.");
}
}
MainWindow::~MainWindow()
{
delete ui;
}
I get this error:
D:\Qt-Projekte\test3\mainwindow.cpp:12: Fehler: no 'void MainWindow::on_confirmButton_clicked()' member function declared in class 'MainWindow'
void MainWindow::on_confirmButton_clicked() {
^
My question is now: how to fix it?
Thanks in advance.
my mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
All member functions of a class must contain a "declaration" and a "definition".
For example:
struct Foo {
// Declarations
void foo() const;
int bar(int x);
};
// Definitions
void Foo::foo() const {
std::cout << "foo" << std::endl;
}
int Foo::bar(int x) {
return x + 1;
}
(Note that if you define a function inline in a class, it counts as both definition and declaration):
struct Foo {
// Declaration AND definition
void foo() const {
std::cout << "foo inline" << std::endl;
}
};
So in your case, you must declare the on_confirmButton_clicked function inside the definition of class MainWindow.
Getting error while declaring QVector of class type in Qt.
Error :"incomplete type is not allowed"
I didn't understand what causes this error.
if i #include "storage.h" in main.cpp and declare a Qvector of class storeage it works fine but when i do the same in waveform class it reports an error.
I've tried forward declaring storage class in waveform class but still getting the same error.
Any Help??
main.cpp
#include "test_subject_02.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TEST_SUBJECT_02 w;
w.show();
return a.exec();
}
test_subject_02.h
#ifndef TEST_SUBJECT_02_H
#define TEST_SUBJECT_02_H
#include <QtGui/QMainWindow>
#include "ui_test_subject_02.h"
#include"waveform.h"
#include "storage.h"
class TEST_SUBJECT_02 : public QMainWindow
{
Q_OBJECT
public:
TEST_SUBJECT_02(QWidget *parent = 0, Qt::WFlags flags = 0);
waveform *wv;
~TEST_SUBJECT_02();
private:
Ui::TEST_SUBJECT_02Class ui;
};
#endif // TEST_SUBJECT_02_H
test_subject_02.cpp
#include "test_subject_02.h"
TEST_SUBJECT_02::TEST_SUBJECT_02(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
QVector<storage> ser; //works fine here
wv->readfile("e:/testing2/result/3/abc.cur");
}
TEST_SUBJECT_02::~TEST_SUBJECT_02()
{
}
waveform.h
#ifndef WAVEFORM_H
#define WAVEFORM_H
#include "storage.h"
#include <QObject>
class waveform : public QObject
{
Q_OBJECT
public:
waveform(QObject *parent=0);
void readfile(QString);
QVector <storage> myvector ; // incomplete type is not allowed
~waveform();
private:
};
#endif // WAVEFORM_H
waveform.cpp
#include "waveform.h"
waveform::waveform(QObject *parent)
: QObject(parent)
{
}
void waveform::readfile(QString file)
{
QVector<storage> sw; //again error incomplete type is not allowed
}
waveform::~waveform()
{
}
storage.h
#ifndef STORAGE_H
#define STORAGE_H
#include <QObject>
class storage : public QObject
{
Q_OBJECT
public:
storage(QObject *parent);
storage();
storage(QString,QString);
~storage();
private:
QString x;
QString y;
};
#endif // STORAGE_H
storage.cpp
#include "storage.h"
storage::storage(QObject *parent)
: QObject(parent)
{
}
storage::storage(QString xcord,QString ycord)
{
x=xcord;
y=ycord;
}
storage::storage()
{
}
storage::~storage()
{
}
You need to explicitly include QVector in necessary file (waveform.h i believe), since QT uses a lot of forward declarations, while they appear as correct classes in IDE, but they won't compile, if proper definition is not included in file.
I've created an SSCE to better explain this.
In a class, I have a variable, a struct, and a declaration of the struct. Within that struct is a constructor, a variable, a struct and a declaration of that struct. And inside THAT struct is a constructor.
So is goes Mother > Daughter > GDaughter aka class > struct > struct
mother.h
#ifndef MOTHER_H
#define MOTHER_H
#include <QSplitter>
class Mother : public QSplitter
{
Q_OBJECT
public:
Mother(int);
int age;
struct Daughter
{
Daughter();
int height1;
struct GDaughter
{
GDaughter();
};
GDaughter *kate;
};
Daughter *tina;
};
#endif // MOTHER_H
Now let's take a look at the constructors/source. Here lies my issue.
mother.cpp
#include "mother.h"
#include <QDebug>
Mother::Mother(int a)
{
age = a;
tina = new Daughter();
}
Mother::Daughter::Daughter()
{
qDebug() << age; //Not going to work... I get it. Daughter isnt a derived class
height1 = 10;
kate = new GDaughter();
}
Mother::Daughter::GDaughter::GDaughter()
{
qDebug() << height1; //Why not? The GDaughter instance is a member of Daughter!
}
Both qDebug() lines throw is not a type name, static, or enumerator.
The goal is to create "child" structs dynamically. So a parent struct might have 0 child structs, 1 child struct, or even 100 child structs. That is why I am using structs instead of derived classes. This setup looks like it would work except for the problem where "parent" variables can't be accessed.
I will include the other files anyway:
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mother.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mom = new Mother(50);
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "mother.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Mother *mom;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
If I am misunderstanding how to go about making something like this then let me know.
Thanks for your time.
AFTER ANSWER
In the mother.h I added parent pointers:
struct Daughter
{
Daughter(Mother *p); //Here
int height1;
struct GDaughter
{
GDaughter(Daughter *p); //And here
};
GDaughter *kate;
};
and in mother.cpp I filled in the needed code:
Mother::Mother(int a)
{
age = a;
tina = new Daughter(this); //Here
}
Mother::Daughter::Daughter(Mother *m) //Here
{
qDebug() << m->age; //Here
height1 = 10;
kate = new GDaughter(this); //Here
}
Mother::Daughter::GDaughter::GDaughter(Daughter *d) //Here
{
qDebug() << d->height1; //Here
}
keep pointer(std::weak_ptr if you use smart pointers, for example) to parent class object in nested object.
explicit MainWindow(QWidget *parent = 0);
look at Qt code here, you pass pointer to the parent object in constructor,
if parent is nullptr that means MainWindow has no parent object, Also you can keep
pointer to MainWindow parent object and then static_cast it to exact class
#include <iostream>
class A {
class B{
public:
B(A *p):parent(p) { parent->hello(); }
private:
A *parent;
};
public:
A() { b = new B(this); }
void hello() { std::cout << "hello world" << std::endl; }
private:
B *b;
};
Hello I keep getting this
error: expected class-name before '{' token
{
^ line 15
what does this error mean exactly? Im attempting to inherit the controller class that has a function that I need to call in form1's cpp.
#ifndef FORM1_H
#define FORM1_H
#include <QDialog>
#include "controller.h"
namespace Ui {
class form1 ;
}
class form1 : public QDialog, public controller
{
Q_OBJECT
public:
explicit form1(QWidget *parent = 0);
~form1();
private slots:
void on_pushButton_clicked();
private:
Ui::form1 *ui;
};
#endif // FORM1_H
the controller class
#include "controller.h"
#include "ui_controller.h"
controller::controller(QWidget *parent) :
QWidget(parent),
ui(new Ui::controller) {
ui->setupUi(this);
show(1); }
void controller::show(int x) {
if(x==1)
{
myform1 = new form1(this);
myform1->show();
}
if(x==2)
{
myform2 = new form2(this);
myform2->show();
}
if(x==3)
{
myform3 = new form3(this);
myform3->show();
} }
controller::~controller() {
delete ui; }
controller.h:
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <QWidget>
#include <form1.h>
#include <form2.h>
#include <form3.h>
namespace Ui {
class controller;
}
class controller : public QWidget
{
Q_OBJECT
public:
form1 * myform1;
form2 * myform2;
form3 * myform3;
void show(int x);
explicit controller(QWidget *parent = 0);
~controller();
private:
Ui::controller *ui;
};
#endif // CONTROLLER_H
My guess is you have a circular include issue, meaning controller.h includes, either directly or indirectly, form1.h.
EDIT: Change the include I was talking about to a forward declaration - you don't need the full definition of form1:
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <QWidget>
class form1;
class form2;
class form3;
namespace Ui {
class controller;
}
class controller : public QWidget
{
//.................
//.................