External Variables not transferring
-
Hi, I'm trying to create a program that takes inputs from an external file "C:/Repo/GraphTestData.txt". This takes place in my main.cpp file, where it is parsed into two double QVectors x and y. Then, I externally export x and y to a shared header "data.h", where it gets accessed in another file named "networkplot.cpp". Here, I use QCustomPlot to plot the data onto a graph. However, the console says that data.h is not used directly by either of the cpp files. How can I fix this?
// data.h #ifndef DATA_H #define DATA_H #include <QVector> extern QVector<double> x; extern QVector<double> y; #endif // DATA_H
#ifndef NETWORKPLOT_H #define NETWORKPLOT_H #include <QMainWindow> #include <QVector> QT_BEGIN_NAMESPACE namespace Ui { class networkplot; } QT_END_NAMESPACE class networkplot : public QMainWindow { Q_OBJECT public: networkplot(QWidget *parent = nullptr); ~networkplot(); private: Ui::networkplot *ui; }; #endif // NETWORKPLOT_H
#include "networkplot.h" #include "ui_networkplot.h" #include <QDebug> #include "data.h" networkplot::networkplot(QWidget *parent) : QMainWindow(parent) , ui(new Ui::networkplot) { ui->setupUi(this); extern QVector<double> x, y; /*QVector<double> x(100),y(100); for(int i=0;i<100;i++) { x[i] = i/50.0 - 1; // x goes from -1 to 1 y[i] = x[i]*x[i]; // quadratic function }*/ ui->customplot->addGraph(); ui->customplot->graph(0)->setScatterStyle(QCPScatterStyle::ssDot); ui->customplot->graph()->setLineStyle(QCPGraph::lsLine); ui->customplot->xAxis->setLabel("X"); ui->customplot->yAxis->setLabel("Y"); ui->customplot->yAxis->setRange(-6000,100); ui->customplot->yAxis->setRange(-6000,8000); ui->customplot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables); ui->customplot->graph(0)->setData(x,y); ui->customplot->rescaleAxes(); ui->customplot->replot(); ui->customplot->update(); } networkplot::~networkplot() { delete ui; }
#include "networkplot.h" #include <QApplication> #include <QFile> #include <QTextStream> #include <QVector> #include <QDebug> #include "data.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); // File path QString filePath = "C:/Repo/GraphTestData.txt"; // Vectors to store data QVector<double> x; QVector<double> y; // Open the file QFile file(filePath); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "Error opening file:" << file.errorString(); return -1; } // Read the file line by line QTextStream in(&file); while (!in.atEnd()) { // Read each line QString line = in.readLine(); // Split the line into two doubles QStringList parts = line.split(" "); if (parts.size() == 2) { bool conversionX, conversionY; double numberX = parts[0].toDouble(&conversionX); double numberY = parts[1].toDouble(&conversionY); // Check if conversion was successful if (conversionX && conversionY) { // Append to vectors x.append(numberX); y.append(numberY); } else { qDebug() << "Error converting line to numbers:" << line; } } else { qDebug() << "Invalid line format:" << line; } } // Close the file file.close(); // Print the content of vectors qDebug() << "Vector x:" << x; qDebug() << "Vector y:" << y; networkplot w; w.show(); return a.exec(); }
It's my first time working with C++, so I would greatly appreciate some help!
-
@thinkingcow
I think you can find more information for the "fix available" by hovering over the warning message or clicking something.Anyway,
data.h
declares that there are two global variables somewhere,x
andy
(btw really bad names for global variables).But in fact no such global variables are actually defined anywhere in your code (might be in a
data.cpp
but you don't show any such file). And the 2extern
variables are not referenced anywhere.networkplot::networkplot
has its ownextern
statement for them,main()
defines anx
&y
but they are local variables, not globals.You may be new to C++ but presumably not to C, else you would likely not know about
extern
. So the code question you have is really about C rather than C++.Don't use any
extern
here, you don't need it. By the look of things, passx
&y
as parameters from where they are defined inmain()
to yournetworkplot
class instance. Probably as extra parameters to the constructor. Or via a separate method,plot()
, which you call after construction that does just theui->customplot->graph(0)->setData(x,y);
part.