initializing class A (with a reference member) within class B - c++

i'm trying to make a simple currency converter and I have some problems with my class designed to make the calculations.
I want to create an object of this class in my MainWindow to use the total variable for displaying the result in a widget on my appi form.
Member functions aren't all here yet, but the problem I have is that i get a
'Calculator' : no appropriate default constructor available error in mainwindow.cpp
While I do know what that means, I don't know how to avoid (solve) the issue because i kind of need the reference on the map to get the echange rates needed.
.cpp
#include <QObject>
#include <QString>
#include <QDebug>
#include <ui_mainwindow.h>
class Calculator
{
public:
explicit Calculator(QMap<QString,double> &currency_map);
void multiply(double x, double y);
void getValues(QString strFrom, QString strTo);
private:
double total, firstCurr, secondCurr;
QMap<QString,double> &map;
};
.h
#include "calculator.h"
Calculator::Calculator(QMap<QString,double> &currency_map):map(currency_map)
{
total = 0;
firstCurr = 0;
secondCurr= 0;
}
void Calculator::getValues(QString strFrom, QString strTo)
{
QMapIterator<QString, double> i(map);
while(i.hasNext())
{
if(i.key() == strFrom)
firstCurr=i.value();
if(i.key() == strTo)
secondCurr = i.value();
}
}
void Calculator::multiply(double x, double y)
{
total = x * y;
}
and now im trying to create an object of that class in my MainWindow class:
#include <QMainWindow>
#include <ui_mainwindow.h>
#include "calculator.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QMap<QString,double> &currency_map, QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
parser currency_parser;
Calculator calc; // error
};
MainWindow.h
MainWindow::MainWindow(QMap<QString, double> &currency_map, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->from_Combox->addItems(currency_parser.currency_list);
ui->to_Combox->addItems(currency_parser.currency_list);
}
the idea behind was that i have a Map in my main in which i save all my data needed and pass the map around to classes that use it.
i'll post my main even tho im not sure if it's needed
#include "downloader.h"
#include "mainwindow.h"
#include "parser.h"
#include "calculator.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMap<QString,double> currency_map;
downloader d;
d.Do_download();
parser p;
p.read_line(currency_map);
MainWindow w(currency_map);
w.show();
return a.exec();
};

The error occurs, because you don't specify the appropriate constructor for class Calculator in the MainWindow constructors member initializer list (the compiler want's to use the default constructor then):
MainWindow::MainWindow(QMap<QString, double> &currency_map, QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, calc(currency_map) // <<<<<<<<<<<<<<
{
// ...
}

Related

Using object directly from another class c++

Hi i have 3 classes that i want to use. but i dont want to create object of one class more than once. I directly want to use the object (in third class) of one class declared and initialized in second class.
To understand the problem please focus on NetworkConnection members and class defined in example below.
Class Mainwindow header
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "secondscreen.h"
#include "networkconnection.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
NetworkConnection *NetworkConnectionObject;
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
SecondScreen* SecondScreenObject;
};
#endif // MAINWINDOW_H
Class Main Window cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QMessageBox"
#include "networkconnection.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
NetworkConnectionObject = new NetworkConnection();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
int Error = NetworkConnectionObject->Connect(Username,Password);
///////////////
// This Works
//////////////
NetworkConnectionObject->LogInToken = "";
}
Class NetworkConnection Header
#ifndef NETWORKCONNECTION_H
#define NETWORKCONNECTION_H
#include <QString>
class NetworkConnection
{
public:
NetworkConnection();
int Connect(QString Username, QString Passwd);
QString LogInToken;
};
#endif // NETWORKCONNECTION_H
Now i want to use Networkclassobject directly in SeconScreenclass so that i can access LogInToken Member of MainWindowInstance.
#include "secondscreen.h"
#include "ui_secondscreen.h"
#include "mainwindow.cpp"
SecondScreen::SecondScreen(QWidget *parent) :
QDialog(parent),
ui(new Ui::SecondScreen)
{
ui->setupUi(this);
///////////////
// This doesnot work
//////////////
MainWindow::NetworkConnectionObject->LogInToken = "";
}
SecondScreen::~SecondScreen()
{
delete ui;
}
However, when i try this compiler says
Invalid use of non-static data member "MainWindow::NetworkConnectionObject" Problem is i dont want to declare it static. Is there any way to do it.
Without seeing secondscreen.h I can't say for sure but MainWindow::NetworkConnectionObject" doesn't reference a specific instance of MainWindow class, you must reference a specific instance to modify.
If in your SecondScreen header you have something like
class SecondScreen {
// . . .
MainWindow window;
// . . .
}
then in your constructor for SecondScreen you must use the initialized instance of MainWindow, i.e.
SecondScreen::SecondScreen(QWidget* parent) : QDialog(parent) {
window.NetworkConnectionObject->LogInToken = "";
}

Qt strange debugger error QWidget and QApplication

This happens when i enter the code to the slots of my buttons
when i debug the app normally ,all functions etc everything is ok.
but when i try use my code in UI something brokes down.
I'm making the Parking System for my uni classes ,
i got one parent class vehicle and 4 child classes which inherits by public.
here's the code from the UI source file :
#include "datain.h"
#include "ui_datain.h"
#include <QMessageBox>
#include <car.h>
#include <bike.h>
#include <motorbike.h>
#include <tir.h>
#include <vehicle.h>
#include <iostream>
using namespace std;
dataIn::dataIn(QWidget *parent) :
QDialog(parent),
ui(new Ui::dataIn)
{
ui->setupUi(this);
}
dataIn::~dataIn()
{
delete ui;
}
dataIn siema;
void dataIn::on_lineEdit_textEdited(const QString &arg1)
{
siema.rejestracja=arg1;
}
void dataIn::on_lineEdit_2_textEdited(const QString &arg1)
{
siema.marka=arg1;
}
void dataIn::on_car_clicked()
{
Vehicle *wsk;
string reje,mareczka;
reje=siema.rejestracja.toStdString();
mareczka=siema.marka.toStdString();
Car obj(reje,mareczka);
wsk=&obj;
wsk->add();
}
void dataIn::on_motorcycle_clicked()
{
Vehicle *wsk;
string reje,mareczka;
reje=siema.rejestracja.toStdString();
mareczka=siema.marka.toStdString();
Motorbike obj(reje,mareczka);
wsk=&obj;
wsk->add();
}
void dataIn::on_bike_clicked()
{
Vehicle *wsk;
string reje,mareczka;
reje=siema.rejestracja.toStdString();
mareczka=siema.marka.toStdString();
Bike obj(reje,mareczka);
wsk=&obj;
wsk->add();
}
void dataIn::on_tir_clicked()
{
Vehicle *wsk;
string reje,mareczka;
reje=siema.rejestracja.toStdString();
mareczka=siema.marka.toStdString();
Tir obj(reje,mareczka);
wsk=&obj;
wsk->add();
}
Here's the main:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
dataIn code:
#ifndef DATAIN_H
#define DATAIN_H
#include <QString>
#include <iostream>
#include <QDialog>
using namespace std;
namespace Ui {
class dataIn;
}
class dataIn : public QDialog
{
Q_OBJECT
QString rejestracja;
QString marka;
public:
explicit dataIn(QWidget *parent = 0);
~dataIn();
private slots:
void on_lineEdit_textEdited(const QString &arg1);
void on_lineEdit_2_textEdited(const QString &arg1);
void on_car_clicked();
void on_motorcycle_clicked();
void on_bike_clicked();
void on_tir_clicked();
private:
Ui::dataIn *ui;
};
#endif // DATAIN_H
You may not declare
dataIn siema;
and you should not refer to it - remove all siema.... references from your dataIn functions (replace siema. with nothing or with this->).
When you need your dialog for the first time, create it in (and as child of) MainWindow:
void MainWindow::whateverEvent() {
dataIn *siema = new dataIn(this);
siema->show();
}
If you need to reference it again or want do prevent it from being created twice, store the pointer in a member of your MainWindow.

accessing a class member from another class in c++

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

Error while declaring QVector of class type

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.

C++ accessing object, QT

I'd appreciate it, when someone could help me. I am not used to C++. I am new to it.
My problem: I want to make an object "player" usable for another class. I don't get how to archive this.
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
#include <QMainWindow>
#include <QMediaPlayer>
#include <QDebug>
#include "Leap.h"
#include <leapmotionlistener.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QMediaPlayer* player;
private slots:
....
private:
Ui::MainWindow *ui;
void initialize();
Controller controller;
LeapMotionListener leapMotionListener;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSound>
#include <iostream>
#include <QfileDialog>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
player = new QMediaPlayer(this);
connect(player, &QMediaPlayer::positionChanged, this, &MainWindow::on_positionChanged);
connect(player, &QMediaPlayer::durationChanged, this, &MainWindow::on_durationChanged);
initialize();
}
QString path;
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initialize(){
controller.addListener(leapMotionListener);
leapMotionListener.setPlayer(player);
controller.setPolicy(Leap::Controller::POLICY_BACKGROUND_FRAMES);
}
...
leapmotionlistener.h
#ifndef LEAPMOTIONLISTENER
#define LEAPMOTIONLISTENER
#include <iostream>
#include <cstring>
#include "Leap.h"
#include <QFile>
#include <QAudioOutput>
#include <QMediaPlayer>
using namespace Leap;
class LeapMotionListener : public Listener {
public:
LeapMotionListener();
void setPlayer(QMediaPlayer&);
//... some more methods
private:
QMediaPlayer player;
};
#endif // LEAPMOTIONLISTENER
leapmotionlistener.cpp
//calls the player object in one method like the following
player.stop();
My main questions are: What am I doing wrong while referencing and instantiating? And how can leapmotionlistener access the same player object (I want to influence the audio)?
I get the Error:
MusicPlayer\mainwindow.cpp:32: Error: C2664: 'void LeapMotionListener::setPlayer(QMediaPlayer &)' : converting from argument 1 'QMediaPlayer *' in 'QMediaPlayer &' not possible
This little project is downloadable for a quick view with the following link:
https://www.dropbox.com/s/igr7ywnvicdlxxd/MusicPlayer.zip?dl=0
Thanks in advance!
All you need to do is
leapMotionListener.setPlayer(*player);
An lvalue reference binds to an object, not to the pointer to the object. In order to get the object from a pointer to it, you need to dereference the pointer using the *.
Among other problems, the QMediaPlayer you're trying to pass to setPlayer is a pointer, not a reference to an object.
You can either instantiate your player object like this:
QMediaPlayer player;
Or you can change your setPlayer function signature to this:
void setPlayer(QMediaPlayer* mplayer);
If you update your function signature, you'll also need fix other function signatures and leave the ampersands out of your connect() statements when you're referencing the player.
the following worked now:
in MainWindow.h:
public:
QMediaPlayer* player;
in MainWindow.cpp:
player = new QMediaPlayer(this);
leapMotionListener.setPlayer(player);
controller.addListener(leapMotionListener);
in LeapMotionListener.h:
private:
QMediaPlayer *player;
public:
void setPlayer(QMediaPlayer *mplayer);
in LeapMotionListener.cpp:
LeapMotionListener::LeapMotionListener()
: player(0)
{}
void LeapMotionListener::setPlayer(QMediaPlayer *mplayer){
player = mplayer;
}