Hi,
I have a function which I need in different source files again and again. So I thought implementing it in an own class (Defaults) as static function. I have
andCode://Defaults.h #ifndef DEFAULTS_H #define DEFAULTS_H class Defaults { public: static const double c; static void read(); }; #endif
And then somewhere in my code I want to do:Code://defaults.cpp #include "defaults.h" #include <iostream> using namespace std; static void Defaults::read() //this is line no. 6 { cout << "static read"; }
When trying to compile I getCode://some other class.cpp #include "mainwindow.h" #include "defaults.h" #include <iostream> using namespace std; MainWindow::MainWindow() { cout << "construtctor mainwindow"; cout << Defaults::c; // c was set before this works Defaults::read(); // this doesn't work }
So what is wrong here?defaults.cpp:6: error: cannot declare member function ‘static void Defaults::read()’ to have static linkage
Apart from the bug itself here, is that the right way to achieve what I want to achieve?
Thanks, J.




Reply With Quote