Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Static Variable in C++

Static Variable in C++

Static Variable in C++

I have the follwing class defined in X.cpp

class X {
  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 "X.cpp"

void main(void) {
// I call the static method of X here

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

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

What cab be the problem?

RE: Static Variable in C++

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close