PDA

View Full Version : Calling a method from a different .cpp file



VitaminG
20th January 2017, 20:20
I want to save some data as a text file, the first txt file will contain header information, the other text file will save data streamed from sensors, so with the help from the internet I created the following "datalogger.cpp" file




#include "datalogger.h"
#include <QDebug>
#include <iostream>
#include <QFile>

DataLogger::DataLogger(QObject *parent) : QObject(parent)
{

}

DataLogger::~DataLogger(){

}
void DataLogger::save(DataStream &input){
saveAsText(input);


}
void DataLogger::saveAsText(DataStream &input){

QTextStream outHeader(&outFileHeader);

outHeader << "[CAPTURE SETTINGS]\n"
<< "Filename: " << SettingsSingleton::instance().getFileName() << ".txt \n"
<< "Samples: " << QString::number(input.size()) << "\n"
<< "Duration: " << QString::number(input.back().time) << "ms \n"
<< "Sample rate: " << QString::number(SettingsSingleton::instance().getS ampleRate()) << " Hz\n"
<< "Source: " << SettingsSingleton::instance().getSource() << "\n"

outFileHeader.close();

}

QFile outFile(SettingsSingleton::instance().getFileName( ) + ".txt");

QTextStream out(&outFile);

for (int i ; i<input.size();i++){
const EcgStreamObject tmp=input.at(i);
out << tmp.toText() << endl; //"\n";

}

outFile.close();
}

}



I have my "DataStream" input variable that I want to pass to the method and save as a ".txt" file, however I do no know how to call the method "void DataLogger::save(DataStream &input)" from a different ".cpp" file where the DataStream variable is located.
I am extremely new to c++ please it as simple as possible please.
Thank you in advance

ChrisW67
20th January 2017, 20:30
#include "datalogger.h"
...
DataStream myDataStream;
...
DataLogger logger;
logger.save(myDataStream);

This assumes that DataLogger::save() is declared public.

VitaminG
20th January 2017, 20:56
How would I know if it is declared as public?
Orhow would I declare it as public?
Thanks for your reply

d_stranz
20th January 2017, 21:27
I think that instead of asking a lot of questions here about basic C++ things that you should already know -before- you start trying to use Qt, you should work your way through a C++ book or some of the many online C++ tutorials. Here is a good one (http://www.cplusplus.com/doc/tutorial/), on a site I use all the time for C++ information.

This really is a forum for answering questions about Qt, not for teaching basic C++ concepts.