Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Static Variable in C++

Status
Not open for further replies.

benaam

Programmer
Jan 11, 2001
20
US
I have the follwing class defined in X.cpp

class X {
public:
static bool bl_PropFile;

void static writeToFile() {
if(bl_PropFile == true) {
// do something
} else if(bl_PropFile == false) {
// do something
}
}
};
bool MonitorCode::bl_PropFile;

Then I have another file called Y.cpp as shown below:

#include <iostream>
#include<stdio.h>
#include &quot;X.cpp&quot;

void main(void) {
// I call the static method of X here
X::writeToFile();
}

both the files are compiling fine. But when I am building
them I am getting the following error:

X.obj : error LNK2005: &quot;public: static bool X::bl_PropFile&quot; (?bl_PropFile@X@@2_NA) already defined in Log.obj


What cab be the problem?

 
It looks like class X has already been defined in another file (Log). You either have to change the name of the class in X.cpp or define a namespace.
James P. Cottingham
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top