-
Global variables
I know it is bad practice to use globals, so please don't lecture me. However, I am using one global variable and it is doing something wierd. I have a value input from a file to it, and its value works fine throughout that function. As soon as the program leaves that function (and moves to a different cpp file) the value of the variable is lost and becomes 0. I was wondering if anyone knows the cause of this?
The flow is such:
data_driver.cpp calls a function in In_out.cpp
this function has a line that inputs a number from the file and sets a variable to this number
In_out.cpp eventually returns, and in data_driver.cpp the value of that variable is now 0
-
Maybe you accidentally defined more than one variable with the same name.
-
In the other file you might need
Code:
extern type variable;
[edit]
You probably have
and left out the extern. That declares another variable of the same name, which is, of course, undeclared.
[/edit]
-
um... why are you including a .cpp file, and why is there a function in it...?
secondly, don't use global variables. pass the variable to the function using a reference, or have the function return a value, then use it in an assignment statement.
-
Ok, sorry, let me clear it up a bit.
The variable I'm dealing with is declared in a header file. This header file is included in both data_driver.cpp and In_out.cpp.
data_driver, like its name, is the driver for the program at the moment, In_out.cpp is a file that has the definitions of a few functions (declared in In_out.h which is included in data_driver.cpp)
So, data_driver calls a function in In_out.cpp. This function opens a file, reads in a number, and sets it to p.nummark (p is the stuct declared in the header file as global with one of its members being nummark)
p.nummark then has the correct value until it exits the function in In_out.cpp, so when I output p.nummark right after the function call in data_driver.cpp it has a value of 0 instead of the value it had in the function in In_out.cpp
-
where did you instantiate the struct?
code would be helpful too...
-
header.h:
Code:
struct paramvar
{
int nummark;
double loc_net_dist;
};
static paramvar p;
data_driver.cpp:
Code:
#include <iostream>
#include "In_out.h"
#include "header.h"
using namespace std;
int main()
{
datavar data;
Get_Genetic_Distances(data);
cout << "nummark is " << p.nummark << endl;
return 0;
}
In_out.cpp
Code:
#include "header.h"
//...
void Get_Genetic_Distances(datavar &data)
{
//...
inFile >> p.nummark;
cout << "Number of marks is " << p.nummark << endl;
//...
}
I didn't put it all here, but there are no other assignment statements for p.nummark
this just seems to be the relevant code
the cout statement in In_out.cpp outputs 'Number of marks is 200'
the cout stament in data_driver.cpp outputs 'nummark is 0'
-
you're getting screwed over by the globalization... what you did was instantiate paramvar in two different places...
what you need to do is include header.h in data_driver.cpp, and instantiate in main. then rewrite Get_Genetic_Distances to either take a reference to an int as it's argument, or return an int.
reworked, your files look like this (I'm using references):
header.h
Code:
struct paramvar
{
int nummark;
double loc_net_dist;
};
data_driver.cpp
Code:
#include <iostream>
#include "In_out.h"
#include "header.h"
using namespace std;
int main()
{
parmvar p;
datavar data;
Get_Genetic_Distances(data,p.nummark);
cout << "nummark is " << p.nummark << endl;
return 0;
}
in_out.cpp
Code:
//...
void Get_Genetic_Distances(datavar &data,int&p)
{
//...
inFile >> p;
cout << "Number of marks is " << p << endl;
//...
}