非静态数据存储器的使用无效 [英] Invalid use of non-static data memeber

查看:489
本文介绍了非静态数据存储器的使用无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ah中有以下代码:

  class A 
{
public:
...
private:
...
const int X = 1;
float Y [X];
...
};

如果只有标题A.h包含在项目中,但是如果我包含Ah在我们说A.cpp它给我奇怪的错误如下:


...警告:非静态数据成员初始化器仅适用于
-std = c ++ 11或-std = gnu ++ 11 [默认启用]



...错误:无效使用非静态数据成员'A :: X'



...错误:来自此位置


即使我改变了 const int X = 1; (虽然我需要它作为常量参数)

ps如果有意义,我使用Qt 5.1



p.s.s。实际上使它 static 修复错误。

解决方案

您的目的是让一个<$ c $



现在这个类用于创建许多对象。实际上,类应该允许每个对象对于 const 数据成员具有不同的 const 值,因此您不能初始化 const 类中的成员。



const



这就是为什么存在一个特殊的初始化点构造函数初始化列表

现在,如果你允许初始化 const member在构造函数中,那么你可以在构造函数中重新赋值多次,这意味着 const (这意味着,分配一次并保持不变)。



因此, const 成员的初始化应在构造函数之前进行,这在初始化列表中完成。这会给你 const 的成员,如果你想要不同的对象的不同的值。



在构造函数中初始化 i(ii)用于在进入构造函数之前初始化数据成员

  class A 
{
const int i;
public:
A(int ii):i(ii)
{
// i = 5;不能这样做,因为我是`const`
}
};

说的话,如果你想让所有对象共享同一个 const value,那么你可以使用 static const 限定符指向其他人的答案。



正如Mike Seymour指出的,对于数组的分配或初始化 const 变量,你需要具有时间常数,但 consts 内部类不给你编译时常量,因此你必须使用 static const 限定符,这使它成为一个编译时常量。


I have the following code in A.h:

class A
{
public:
    ...
private:
    ...
    const int X = 1;
    float Y[X];
    ...
};

This compiles fine if only header A.h is included in the project. But if I include A.h in let's say A.cpp it gives me strange errors as follows:

... warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]

... error: invalid use of non-static data member 'A::X'

... error: from this location

Even when I change const int X = 1; (though I need it as a constant parameter), nothing changes.

p.s. If it makes sense I am using Qt 5.1

p.s.s. Actually making it static fixes the error. But why I have to do that?

解决方案

Your intention is to have a constant variable inside the class.

Now this class is used to create many objects. Practically the class should allow each object to have different const value for the const data member , thus you can't initialize const members inside class.

const inside class members means the value is constant throughout the life time of the object.

That is why there exist a special initialization point called constructor initialization list rather than allowing the initialization in the constructor.

Now, if you're allowed to initialize the const member inside the constructor then you could re-assign it multiple times within the constructor itself which voilates the meaning of const(which means, assigned once and remains same throughout).

Hence the initialization of the const members should happen before the constructor, which is done in the initialization list. This gives you const members with different values for different objects if you want.

Also, note that the form of initialization in the constructor i(ii) is used to initialize the data members before entering the constructor

class A
{
    const int i;
    public:
       A(int ii): i(ii)
       {
          // i  = 5; Can't do this since i is `const`
       }
};

Having said that, if you want all the object to share the same const value then you can use static const qualifier as pointed in others answers.

Also as Mike Seymour pointed out, for allocation of arrays or initializing const variables you need to have comiple-time constants, but consts inside class doesn't give you compile time constants, hence you will have to use the static const qualifier which makes it a compile time constant.

这篇关于非静态数据存储器的使用无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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