PDA

View Full Version : class MainWindow has no member 'customPlot'



Arend
9th March 2013, 10:47
Hello,

When I implement the code below I get the error: 'class MainWindow' has no member named 'customPlot'
What can I do to solve this?

Regards,
Arend (absolute beginner)



mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtCore>
#include <QtGui>
#include "qcustomplot.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_pushButtonSwap_clicked();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QtCore"
#include "QtGui"
#include "QDebug"
#include "QMessageBox"
#include <QVector>
#include <cmath>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButtonSwap_clicked()
{
// generate some data:
QVector<double> x(101), y(101);
for (int i=0; i<101; ++i) {
x[i] = i/50.0 - 1; // x goes from -1 to 1
y[i] = x[i]*x[i]; // let's plot a quadratic function
}
this->customPlot->addGraph();
this->customPlot->graph(0)->setData(x, y);
this->customPlot->xAxis->setLabel("x");
this->customPlot->yAxis->setLabel("y");
this->customPlot->xAxis->setRange(-1, 1);
this->customPlot->yAxis->setRange(0, 1);
this->customPlot->replot();
}

Ashkan_s
9th March 2013, 11:37
Try removing the keyword this before customPlot
void MainWindow::on_pushButtonSwap_clicked()
{
// generate some data:
QVector<double> x(101), y(101);
for (int i=0; i<101; ++i) {
x[i] = i/50.0 - 1; // x goes from -1 to 1
y[i] = x[i]*x[i]; // let's plot a quadratic function
}
customPlot->addGraph();
customPlot->graph(0)->setData(x, y);
customPlot->xAxis->setLabel("x");
customPlot->yAxis->setLabel("y");
customPlot->xAxis->setRange(-1, 1);
customPlot->yAxis->setRange(0, 1);
customPlot->replot();
}
if customPlot is defined in one of the header files, if not you need to declare an object of type QCusomPlot named customPlot in MainWindow class.


this->customPlot means that the class MainWindow has a member named customPlot, and in this case it does not exists.

Arend
9th March 2013, 11:49
Thanks for your reply but when I remove this then i get:
/Users/..../mainwindow.cpp:33: error: 'customPlot' was not declared in this scope

Ashkan_s
9th March 2013, 11:55
Declare it in MainWindow class

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_pushButtonSwap_clicked();

private:
Ui::MainWindow *ui;
QCustomPlot *customPlot;
};

And new it in MainWindow's constructor

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
customPlot = new QCustomPlot(this);
}

Arend
9th March 2013, 12:02
Thanks! Now it works.

amleto
9th March 2013, 12:17
Declare it in MainWindow class

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_pushButtonSwap_clicked();

private:
Ui::MainWindow *ui;
QCustomPlot *customPlot;
};

And new it in MainWindow's constructor

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
customPlot = new QCustomPlot(this);
}

Why did you use pointer and not normal member? Needless use of the heap.

Ashkan_s
9th March 2013, 12:33
Why did you use pointer and not normal member? Needless use of the heap.

To be compatible with the rest of the code;)