小编ATu*_*ngh的帖子

为什么在类内初始化程序中不允许加倍

我一直在阅读Herb Shutter的"Exceptional C++","第1项:#define或const和inlining [...]".

据说,类内初始化仅允许用于整数类型(整数,字符,bool),并且仅用于常量.

我只想知道为什么double/float无法在类声明中初始化.有什么具体原因吗?

class EngineeringConstants {      // this goes in the class
 private:                          // header file
  static const double FUDGE_FACTOR;
  ...
 };
 // this goes in the class implementation file
 const double EngineeringConstants::FUDGE_FACTOR = 1.35;
Run Code Online (Sandbox Code Playgroud)

我只是想知道不允许以下声明的原因:

class EngineeringConstants {      // this goes in the class
 private:                          // header file
  static const double FUDGE_FACTOR = 1.35;
  ...
 };
Run Code Online (Sandbox Code Playgroud)

?

c++ oop

12
推荐指数
1
解决办法
424
查看次数

在C中声明两个同名的全局变量

我在C中声明了两个同名的全局变量.它应该给出错误,因为我们不能在同一个存储类中声明相同的名称变量.

我用C++检查了它 - 它给出了一个编译时错误,但不是在C中.为什么?

以下是代码:

int a;
int a = 25;
int main()
{

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

请查看:Ideone写的代码

我想这可能就是原因

C中的声明和定义

但在C++中并非如此.我认为在C++中,无论变量是在全局范围还是自动范围声明,声明和定义都是同时发生的.

任何人都可以对它投入更多的光.

现在当我定义变量两次给它两次值时,它给了我错误(而不是一个声明和一个定义).

代码:现在有两个定义

int a;
int a;
int a;
int a = 25;

int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)

c c++ scope global

10
推荐指数
1
解决办法
3333
查看次数

VS2015 无法从“初始化列表”转换为“std::string”错误

// get the drive letter/network volume portion of the path
    std::string drive_path;
    if (drive_type == disk_util::drive_type_network)
    {
        boost::iterator_range<std::string::const_iterator> r = boost::find_nth(path, "/", 3);
        if (!r.empty())
            drive_path = std::string(path.begin(), r.begin());
    }
    else
    {
        size_t i = path.find('/');
        drive_path = path.substr(0, i);
    }


//path is also a std::string type 
Run Code Online (Sandbox Code Playgroud)

问题出在这一行:drive_path = std::string(path.begin(), r.begin());

这是在 VS2013 上成功编译,但它在 VS 2015 中抛出错误 Error: error C2440: '': cannot convert from 'initializer list' to 'std::string'

根据 std::string 构造函数,我们有一个 Range Constructor,它接受迭代器 1 和迭代器 2 来填充字符串。

http://www.cplusplus.com/reference/string/string/string/

    //range …
Run Code Online (Sandbox Code Playgroud)

c++ string stdstring visual-studio-2013 visual-studio-2015

2
推荐指数
1
解决办法
1118
查看次数