I have a header file and 3 cpp files. I want to share an array object between my files to store data in it and use it (global variable).
My header file code : info.h
#ifndef INFO_H
#define INFO_H
#include "QString"
class ib
{
public:
QString from,to,seat[32],driver;
}extern o[10];
#endif // INFO_H
and my cpp files code : bilit.cpp
#include "info.h"
void bilit::on_pushButton_clicked()
{
const int f = ui->bnspin->value();
const int s = ui->stspin->value();
o[f].seat[s] = ui->pnbtn->text();
}
make.cpp
#include "info.h"
void make::on_pushButton_clicked()
{
const int f = ui->spinBox->value();
o[f].driver = ui->drvbtn->text();
o[f].to = ui->tobtn->text();
o[f].from = ui->frombtn->text();
}
check.cpp
#include "info.h"
void check::on_pushButton_clicked()
{
const int f = ui->spinBox->value();
const int s = ui->spinBox_2->value();
ui->label->setText(o[f].seat[s]);
}
and this is my error : error
even i write extern in my cpp files but i get error again : error2
any idea how to fix it ? or other way to share a global variable between my files ?
Declaring an object as extern can be done many times. All it means is "this object's linkage data is somewhere else".
Your code doesn't contain the "somewhere else" definition.
file.h:
extern int i; // i is defined somewhere else
file.cpp:
int i = 0; // this is the definition
some-other-file.cpp:
extern int i; // will use the same i in file.cpp
// alternatively, to avoid duplicating the extern declaration,
// you can just include file.h
To summarize, in one of your cpp files, you need to add:
ib o[10];