The relationship between cpp files? How can I obtain and record the value of a variable from one cpp to another?

0 like 0 dislike
8 views
Make two CPP file to collect them from the library by using makefile in LINUX
I need to record the values of the variables discretX, discretY of function1.cpp in function2.cpp
How to do it?

function.h
struct InfaboutChanell { int chanellcount; int analogChanell; int discretChanell; }; void writeCfgFile (); void writeBinaryFile();


function1.cpp
#include "stdafx.h" #include #include #include #include "functions.h" using namespace std; struct Station station; void writeCfgFile(){ ofstream file("fileConfiguration.cfg"); //The information about count of channels struct InfaboutChanell infaboutChanell; infaboutChanell.chanellcount=14; const int analogX =6; infaboutChanell.analogChanell=analogX; const int discretX=8; infaboutChanell.discretChanell=discretX; //check for the amount of channels int sum; sum = infaboutChanell.analogChanell+infaboutChanell.discretChanell; if (sum != infaboutChanell.chanellcount) { cout << "Please enter correct count of channels:" <<'\'; } file<

function2.cpp
#include "stdafx.h" #include #include #include #include #include "functions.h" using namespace std; struct GeneralInf inf; struct InfaboutChanell aboutchanell; void writeBinaryFile(){ // Create Binary file fstream binary_file("D:\\\\Struct\\\\Struct\\\\ComtradeFormat\\\\fileConfiguration.dat",ios::out|ios::binary|ios::app); if(!binary_file) { cout << "Cannot open file.\"; return 1; } int i=0; int n=0; int timestamp =0; for (i=0; i<=generalinformation.ensamp; i++){ n = i+1; binary_file.write((char *) &n, sizeof n); timestamp = i*1000; binary_file.write((char *) ×tamp, sizeof timestamp); // Value of analog channels short analogValue [analogX]; for(int i=0; i [discretX] = {1, 1, 1, 0, 0, 0, 0, 0}; for(int i = 0; i < discretX; ++i) { if(ChValue[i] == 1) m_bytesCount |= 1 << i; } binary_file.write((char *) &m_bytesCount, sizeof m_bytesCount); } binary_file.close();
by | 8 views

4 Answers

0 like 0 dislike
Get globaline variables in the function.h file and through them share data in the *.cpp files.
(so-so Board)
by
0 like 0 dislike
Return value from one function and pass them to another as parameters.
Return 2 values in the structure or through input parameters. Example.
writeCfgFile void(int & directX, int & analogX){ analogX =6; discretX=8; } void writeBinaryFile(int directX analogX int) { } main() { int analog, direct; writeCfgFile(direct analog); writeBinaryFile(direct analog); }

When you need to return a lot of values then they are combined in the structure and in exactly the same way you fill the structure.
Global variables are, of course, is simple and works, but it's not the right approach.
by
0 like 0 dislike
In one module a global variable and extern in the other.
by
0 like 0 dislike
Well you can, a pointer to the object to transfer but it is possible if anywhere it is necessary to transmit and change. Get and set properties to describe.
by
110,608 questions
257,186 answers
0 comments
33,708 users