【问题标题】:C++ How to share constants with extern between cpp - Error: storage class specifiedC ++如何在cpp之间与extern共享常量 - 错误:指定存储类
【发布时间】:2016-01-03 16:46:10
【问题描述】:

我有一个头文件,其中我用 extern 声明了一些常量:

#ifndef CONSTANTS_H
#define CONSTANTS_H

#include <string>

class Constants
{
    public:
        Constants(){}
        extern const std::string DELIMITER;
        extern const std::string FILENAME;
        extern const int BLUCAR;
        extern const int REDCAR;
        extern const int EMPTY;
        extern const int SPARSE_LIM;
    protected:
    private:
};

#endif // CONSTANTS_H

然后在源代码中我这样定义它们:

#include "constants.h"

extern const std::string DELIMITER = ",";
extern const std::string FILENAME = "cars.csv";
extern const int BLUCAR = 1;
extern const int REDCAR = 2;
extern const int EMPTY = 0;
extern const int SPARSE_LIM = 5;

为什么编译器给我错误:为“DELIMITER”指定的存储类?

【问题讨论】:

  • 删除类定义、构造函数和访问修饰符错误解决。方法对吗?

标签: c++ compiler-errors constants extern


【解决方案1】:

首先,这些似乎不是班级成员。您使用extern 的方式看起来像是您希望这些是免费的。也许在命名空间中。把他们带出那堂课。

然后,当您定义它们时,省略 extern

在这种情况下,它的意思是“在别处找到这个变量”。你不想在别处找到它。 在这里

// Header
namespace Constants {
   extern const std::string DELIMITER;
   extern const std::string FILENAME;
   extern const int         BLUCAR;
   extern const int         REDCAR;
   extern const int         EMPTY;
   extern const int         SPARSE_LIM;
}


// Source
namespace Constants {
   const std::string DELIMITER  = ",";
   const std::string FILENAME   = "cars.csv";
   const int         BLUCAR     = 1;
   const int         REDCAR     = 2;
   const int         EMPTY      = 0;
   const int         SPARSE_LIM = 5;
}

请记住,您会为 static 对象定义做同样的事情!

【讨论】:

  • 我明白你的意思,但我需要在源文件中定义它们,以便我可以包含仅声明它们的头文件。这样,当我修改一个值时,不需要重新编译包含 constants.h 的文件。我不知道我说的对不对,但我在这里理解:stackoverflow.com/questions/9649405/…
  • @rh0x:是的,在源文件中定义它们。我教你怎么做。
【解决方案2】:

我认为您要么想在类中使用静态成员,要么使用命名空间而不是类,或者将常量放在全局命名空间中。

对于静态成员:

class Constants
{
    public:
        Constants(){}
        static const std::string DELIMITER;
        static const std::string FILENAME;
        static const int BLUCAR;
        static const int REDCAR;
        static const int EMPTY;
        static const int SPARSE_LIM;
    protected:
    private:
};

在你的源文件中

const std::string Constants::DELIMITER = ",";
const std::string Constants::FILENAME = "cars.csv";
const int Constants::BLUCAR = 1;
const int Constants::REDCAR = 2;
const int Constants::EMPTY = 0;
const int Constants::SPARSE_LIM = 5;

但是,您的类看起来更像是命名空间而不是类,因为创建实例没有意义。

但还有另一种选择。您甚至可能不想使用类或命名空间,而只是将它们全部放在全局命名空间中:

// header
extern const std::string DELIMITER;
extern const std::string FILENAME;
extern const int BLUCAR;
extern const int REDCAR;
extern const int EMPTY;
extern const int SPARSE_LIM;

// source
const std::string DELIMITER = ",";
const std::string FILENAME = "cars.csv";
const int BLUCAR = 1;
const int REDCAR = 2;
const int EMPTY = 0;
const int SPARSE_LIM = 5;

【讨论】:

  • 是的,你是对的!正如“轨道上的轻盈竞赛”所说,最好的选择可能是使用命名空间。谢谢!
  • @rh0x:请注意,如果您想以某种方式对它们进行分组,那么您只需要一个命名空间,这样您就可以使用命名空间前缀进行访问。如果您只是希望能够使用DELIMITER 而不是Constants::DELIMITER,那么将它放在全局命名空间中可能就是您想要的。
猜你喜欢
  • 1970-01-01
  • 2012-03-19
  • 2010-11-28
  • 2011-03-09
  • 1970-01-01
相关资源
最近更新 更多