-2

I want to store some values / variables so that I can change and read them in any class.

What I tryed so far: I created a header file called "settings.h":

#ifndef SETTINGS_H
#define SETTINGS_H

#include <QString>

class Settings {
public:
    static QString OutputFormat;
};

#endif // SETTINGS_H

Now I included it in a class with:

#include "settings.h"

But when I try to set this variable:

Settings::OutputFormat = "mp3";

It will not compile with that error:

undefined reference to `Settings::OutputFormat'

What to do? I need something like a settings class. Any class which #include "settings.h" should be able to read the variable values and change them. The values should be global and shared between all classes which include this settings class.

Bleuzen
  • 131
  • 1
  • 7

1 Answers1

2

You need to declare a "location" for this variable in a cpp file somewhere:

QString Settings ::OutputFormat;

And this is enough. Without a "location" in a source file, the compiler will not create the variable and it will be an undefined reference.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62