[英]C++ How to share constants with extern between cpp - Error: storage class specified
I've the header file in which I declare some constants with extern: 我在头文件中用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
Then in the source I define them like this: 然后在源代码中按如下方式定义它们:
#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;
Why the compiler gives me Error: storage class specified for 'DELIMITER'? 为什么编译器给我错误:为'DELIMITER'指定的存储类?
Firstly, these don't seem to be class members. 首先,这些人似乎不是班级成员。 The way you're using extern
it looks like you intended for these to be free. 您使用extern
的方式看起来像是希望这些都是免费的。 Perhaps in a namespace. 也许在命名空间中。 Take them out of that class. 把他们带出那堂课。
Then, when you define them, leave out the extern
. 然后,在定义它们时,省去extern
。
In this context it means "find this variable elsewhere". 在这种情况下,它的意思是“在其他地方找到该变量”。 You don't want to find it elsewhere. 您不想在其他地方找到它。 It's here ! 在这里 !
// 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;
}
Remember, you'd do the same for static
object definitions! 请记住,您将对static
对象定义执行相同的操作!
I think you either want to use static members in your class, use a namespace instead of a class, or put your constants in the global namespace. 我认为您要么想在类中使用静态成员,要么使用名称空间而不是类,要么将常量放在全局名称空间中。
For static members: 对于静态成员:
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:
};
and in your source file 并在您的源文件中
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;
However, it looks like your class is more like a namespace than a class, since there would be no point in making instances. 但是,看起来您的类比类更像是名称空间,因为创建实例没有意义。
But there is another option. 但是还有另一种选择。 You may not even want to use a class or a namespace, but just have them all in the global namespace: 您甚至可能不想使用类或名称空间,而只是将它们全部放在全局名称空间中:
// 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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.