2
class Config
{
    public:

        static int OUTPUT_TO_FILE;
        static int NEED_TO_TRAIN;
        static int NO_FILE_TRAIN;
        static int NEED_TO_TEST;
}

Above is my header.h file (i followed: http://weblogs.asp.net/whaggard/archive/2004/11/05/252685.aspx)

and I want to be able to do things like Config.OUTPUT_TO_FILE = true or variable = Config.NO_FILE_TRAIN;

from any file that includes config.h

I have what I want to do is clear, Just want some variable shared throughout all my cpp files.

EDIT: I'm having trouble compiling:

In my make file, I added:

hPif : src/main.o src/fann_utils.o src/hashes.o src/config.o # added the config part
    g++ src/main.o src/fann_utils.o src/hashes.o src/config.o -lfann -L/usr/local/lib -o hPif

config.o : src/config.cpp src/config.h
    g++ -c src/config.cpp

.
.
.
main.o: src/main.cpp src/config.h src/main.h src/hashes.h
    g++ -c src/main.cpp

config.cpp:

#include "config.h"

int OUTPUT_TO_FILE = false;
.
.
.

config.h:

class Config
{
    public:

        static int OUTPUT_TO_FILE;
.
.
.

Trying to call by:

#include "config.h"
...

            Config::OUTPUT_TO_FILE = true;

Error I get:

Undefined Symbols:
  "Config::OUTPUT_TO_FILE", referenced from:
      _main in main.o
      _main in main.o
5
  • possible duplicate of C++ Initialize class static data member Commented Apr 1, 2011 at 14:18
  • These variables look like bool, not int. Commented Apr 1, 2011 at 14:20
  • @Beta: on many systems there is no bool. If want use platform independent code without many defines, easier to use other type than bool. Commented Apr 1, 2011 at 14:31
  • Where did you try it? Config::OUTPUT_TO_FILE = true; If in main.cpp you should add #include "Config.h". Commented Apr 1, 2011 at 15:04
  • capital? I'm including the lowercase one. Commented Apr 1, 2011 at 15:08

4 Answers 4

8

Header (Config.h):

#pragma once

class Config
{
public:
  static int OUTPUT_TO_FILE;
  static int NEED_TO_TRAIN;
  static int NO_FILE_TRAIN;
  static int NEED_TO_TEST;
};

Source (Config.cpp):

#include "Config.h"

int Config::OUTPUT_TO_FILE = 0;
int Config::NEED_TO_TRAIN = 0;
int Config::NO_FILE_TRAIN = 0;
int Config::NEED_TO_TEST = 0;

Usage (any.cpp):

#include "Config.h"

...
int variable = Config::OUTPUT_TO_FILE;
...
Sign up to request clarification or add additional context in comments.

4 Comments

Adding somedetails, as I am having trouble compiling.
@DerNalia: Config.h and Config.cpp are ready and fully detailed.
I think the problem may have my case... should be Config.h, not config.h
@DerNalia: Yes, in un*x match case is important.
0

In one of your source files, add something like this:

int Config::OUTPUT_TO_FILE = 1;

Now, it will be initialized on program startup. Only do this in one source .cpp file, not in multiple.

Comments

0

Because the class only has statics, you should be able to access them in any file which includes this header, for example..

foo.cpp

#include "Config.h"

void bar()
{
  Config::NO_FILE_TRAIN = 0; // set this value for example
}

bar.cpp

#include "Config.h"

void foo()
{
  Config::NEED_TO_TRAIN = 1; // set this value for example
}

Remember you must have an implementation file that is also compiled which actually defines the statics, i.e.

Config.cpp

int Config::OUTPUT_TO_FILE = 0; // initial value
:  //etc.

1 Comment

Adding some details, as I am having trouble compiling.
0

You might want to implement your Config class as a singleton.

Oh, and if you want to set OUTPUT_TO_FILE = true, you'd be better off declaring OUTPUT_TO_FILE as bool...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.