C ++ 11:初始化静态const类成员 [英] C++ < 11 : Initialize static const class member

查看:194
本文介绍了C ++ 11:初始化静态const类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个主要包含静态内容的类,因为它需要是一个随时可访问且没有实例化的库.无论如何,此类都有一个公共的静态成员,即称为cfg的结构,该成员包含其所有配置参数(主要是通过其静态方法实现的算法的边界和公差).并且在顶部具有const静态成员,该成员与cfg具有相同的类型,但是具有所有默认/常规参数值.我模块的用户可能会加载它,对其进行部分修改,并将其应用为cfg,或用作参考或我所知道的.

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.

现在我似乎根本无法初始化这个家伙.没有实例化(并且它是静态的),初始化将不会在构造函数中发生(无论如何都没有).类内init返回错误,cpp中的init返回声明冲突.这里的前进方向是什么?

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:

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()
{
}

以上错误:

module.cpp:11:20:错误:声明'module_cfg module :: default_cfg'冲突 module_cfg module :: default_cfg = {15,19}; ^ 在module.cpp:8:0包含的文件中: module.h:14:29:错误:'module :: default_cfg'具有先前的声明为'const module_cfg module :: default_cfg' const static module_cfg default_cfg; ^ Makefile.Debug:119:目标"debug/module.o"的配方失败 module.cpp:11:20:错误:类外部的'const module_cfg module :: default_cfg'的声明不是定义[-fpermissive] module_cfg module :: default_cfg = {15,19};

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};

预先感谢

查尔斯

推荐答案

上面的错误是由于您无意中将default_cfg重新声明为在cpp文件中是可变的.

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

在定义中添加const可以解决此问题:

adding const to the definition fixes it:

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

这篇关于C ++ 11:初始化静态const类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆