man*_*ans 4 c++ visual-studio-2010
我有这样的代码:
class MyClass
{
private:
static const int intvalue= 50;
static const float floatvalue = 0.07f;
};
Run Code Online (Sandbox Code Playgroud)
在Visual Studio 2010中,我收到此错误:
Myclasses.h(86): error C2864: 'MyClass::floatvalue : only static const integral data members can be initialized within a class
Run Code Online (Sandbox Code Playgroud)
那么如何在c ++中初始化静态常量float呢?
如果我使用构造函数,每次创建此类的对象时,该变量都被初始化,这是不好的.
显然代码是用Linux上的GCC编译的.
gx_*_*gx_ 12
MyClass.h
class MyClass
{
private:
static const int intvalue = 50; // can provide a value here (integral constant)
static const float floatvalue; // canNOT provide a value here (not integral)
};
Run Code Online (Sandbox Code Playgroud)
MyClass.cpp
const int MyClass::intvalue; // no value (already provided in header)
const float MyClass::floatvalue = 0.07f; // value provided HERE
Run Code Online (Sandbox Code Playgroud)
另外,关心
显然代码是用Linux上的GCC编译的.
这是由于延期.尝试使用标志-std=c++98(或者-std=c++03,或者-std=c++11如果你的版本足够新)-pedantic,你会(正确地)得到一个错误.
| 归档时间: |
|
| 查看次数: |
16929 次 |
| 最近记录: |