-1

So I have a class which has mostly static stuff because it needs to be a library accessible at all times with no instantiation. Anyway, this class has a public static member, a structure called cfg, which contains all of its configuration parameters (mostly boundaries and tolerances for the algorithms implemented by its static methods). And on top it has a const static member, which is a structure of the same type as cfg, but has all the default / usual values for the parameters. Users of my module may load it, modify it in part, and apply it as cfg, or use it as reference, or what do I know.

Now I can't seem to initialize this guy, at all. With no instantiation (and it being static) initialization won't happen in a constructor (there is none anyway). In-class init returns an error, init in the cpp returns a declaration conflict. What's the way forward here ?

Here an example with exact same behavior as I get :

module.h :

#ifndef MODULE_H
#define MODULE_H

typedef struct {
    float param1;
    float param2;
} module_cfg;

class module
{
    public:
        module();
        static module_cfg cfg;
        const static module_cfg default_cfg;

};

#endif // MODULE_H

module.cpp :

#include "module.h"
using namespace std;

module_cfg module::default_cfg = {15, 19};

int main(int argc, char* argv[])
{
    //clog << "Hello World!" << endl << endl;

    return 0;
}

module::module()
{
}

Errors with above :

module.cpp:11:20: error: conflicting declaration 'module_cfg module::default_cfg' module_cfg module::default_cfg = {15, 19}; ^ In file included from module.cpp:8:0: module.h:14:29: error: 'module::default_cfg' has a previous declaration as 'const module_cfg module::default_cfg' const static module_cfg default_cfg; ^ Makefile.Debug:119: recipe for target 'debug/module.o' failed module.cpp:11:20: error: declaration of 'const module_cfg module::default_cfg' outside of class is not definition [-fpermissive] module_cfg module::default_cfg = {15, 19};

Thanks in advance,

Charles

C. THIN
  • 147
  • 3
  • 11
  • It sounds like a variation of singleton, but it'd be better to see the class code and examples of usage – user3159253 Apr 18 '16 at 06:53
  • 1
    Initialization in the cpp file would be the way to go. Please show us a [mcve](/help/mcve) that reproduces the error you see. – MikeMB Apr 18 '16 at 06:55
  • should the default configuration be somehow dependent on `.cfg` ? – user3159253 Apr 18 '16 at 07:14
  • 1
    BTW I fixed it by adding "const" to my declaration in the .cpp - the compiler seems to require it. Question is - why do I have to redeclare type/characteristics in the cpp when all is already in the h file ? Why can't I just write : module::default_cfg = {1000, 1000} directly ? – C. THIN Apr 18 '16 at 07:16
  • Add a constructor to your struct and use it to initialize – bazz-dee Apr 18 '16 at 07:24
  • 1
    @C.THIN: If you have a new question, ask it as a separate question please. – Kerrek SB Apr 18 '16 at 07:28
  • @Kerrek SB [No problem](http://stackoverflow.com/questions/36697711/c-why-does-one-have-to-repeat-the-const-specifier-at-definition-time-if-del) – C. THIN Apr 18 '16 at 15:05

1 Answers1

1

The errors above are due to the fact that you are inadvertently redeclaring default_cfg to be mutable in the cpp file.

adding const to the definition fixes it:

const module_cfg module::default_cfg = {15, 19};
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142