I have a header called filepaths.h that determines the number of static variables:
#ifndef FILEPATHS_H #define FILEPATHS_H class FilePaths { public: static QString dataFolder(); static QString profileFolder(); private: static QString dataFolder_; static QString profileFolder_; }; } #endif
And I have an associated paths.cpp file that initially looked like this:
#include "FilePaths.h" QString FilePaths::dataFolder() { return dataFolder_; } QString FilePaths::profileFolder() { return profileFolder_; }
However, this did not work - I received a "unresolved symbolic error" linker error for all static variables. Therefore, I added these variables to the C ++ file as follows:
#include "FilePaths.h" QString FilePaths::dataFolder_ = ""; QString FilePaths::profileFolder_ = ""; QString FilePaths::dataFolder() { return dataFolder_; } QString FilePaths::profileFolder() { return profileFolder_; }
And it works, but I donβt understand why.
Why do these static variables need to be defined twice? Or maybe I do not define them, but initialize them? But why should this be done? Or should I write my class differently?
this.lau_
source share