How to use same object in multiple modules?

Hello. I'm quite new to programming and for a few days I was trying to figure out how could I use an object I created in, for instance, main.cpp in other module, like mainwindow.cpp. Here's my code:
main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <QtGui/QApplication>
#include "mainwindow.h"

#include "main.h"

int main(int argc, char *argv[])
{

    std::string vardas = "c:/cpp/rental.txt";
    DB db(vardas);
    FotoRental rental = db.loadFotoRental();

    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    
    return a.exec();
}


main.h:
1
2
3
4
5
6
7
8
9
10
11
#ifndef MAIN_H
#define MAIN_H

#include "db.h"
#include "domain.h"

extern DB db;
extern FotoRental rental;


#endif // MAIN_H 


mainwindow.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "main.h"
#include "domain.h"
#include "db.h"


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

    Fotos fotos = rental.getAllFotos();
    Foto foto = fotos[1];
    ui->testas->setText(QString::fromStdString(foto.getName()));
}

MainWindow::~MainWindow()
{
    delete ui;
}


The line in Bold gives me same error twice:
C:\cpp\ketvirta-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\..\ketvirta\mainwindow.cpp:15: error: undefined reference to `rental'


What should I do to fix this? I'm planing to add few more modules in which I will use same objects.
You have to define the object in one of your source files, outside the main function.
Fotos rental;

main.cpp, line 11 FotoRental rental = db.loadFotoRental();
Last edited on
Thanks. It works. But my Teacher told me that I shouldn't use global variables. Right now I am stuck in other place. My code compiles, but I get segfault. I assume it is because *rental and *db stop existing after mainwindow.cpp bit of code. How do I make them last longer?

mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
MainWindow::MainWindow(FotoRental *rental, DB* db) :
    QMainWindow(),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    Fotos fotos = rental->getAllFotos();
    for (int i = 0; i < fotos.size(); i++) {
        ...
    }

    Customers customers = rental->getCustomers();
    for (int i = 0; i < customers.size(); i++) {
        ...
    }
}


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main(int argc, char *argv[])
{

    DB* db = new DB("c:/cpp/rental.txt");
    FotoRental* rental = new FotoRental;
    *rental = db->loadFotoRental();

    QApplication a(argc, argv);
    MainWindow w(rental,db); 
    w.show();
    
    return a.exec();
}


mainwindow.h:

1
2
3
4
5
6
7
8
9
10
class MainWindow : public QMainWindow
{
    Q_OBJECT
    DB* db;
    FotoRental* rental;

public:
    explicit MainWindow(FotoRental* rental, DB* db);
    ~MainWindow();
        ...

you could create an object in your main and then pass it as reference "&" to the functions in the other class.
Last edited on
Ahh, good teacher! You have to initialize MainWindow::rental in the constructor otherwise it will be uninitialized when you try to use it in other member functions.
1
2
3
4
5
6
7
MainWindow::MainWindow(FotoRental *rental, DB* db) :
	QMainWindow(),
	ui(new Ui::MainWindow),
	rental(rental)
{
	ui->setupUi(this);
	...

Last edited on
Thanks a lot. But I think I might be back with some more questions.
Topic archived. No new replies allowed.