外部变量和数组声明问题 C++
extern variable and array declare issue c++
我对外部变量和数组声明有疑问。
如何使用不在可声明文件中的全局变量声明数组。
file1.cpp
const int size = 10;
mainfile.cpp
extern const int size;
void main()
{
int mas[size];
}
int mas[size];
这行有问题。
请大家猜一猜??
你不能。数组大小必须是常量表达式;如果它是一个变量,那么该变量必须是 const
并在同一个翻译单元中初始化,以便它的值可用作常量。
如果您想在多个翻译单元之间共享值,请在 header 中定义并包含它。
C++ 不允许在运行时指定数组的大小。在您的示例中,它当然是在 link 时间指定的,但这对编译器没有帮助。
但是,如果您使用的是 C++14 编译器,并且在某些其他编译器(例如 gcc)上,您可以这样做,但它的可移植性不如动态分配内存,而且比 std::vector<> 更不方便.
供参考:https://isocpp.org/wiki/faq/freestore-mgmt#dynamic-array-len
首先常量是有内部链接的。因此这些声明
file1.cpp
const int size = 10;
和
mainfile.cpp
extern const int size;
指的是不同的实体。
在file1.cpp中声明的常量在其对应的编译单元之外是不可见的。
根据 C++ 标准(3.5 程序和链接)
3 A name having namespace scope (3.3.6) has internal linkage if it is
the name of
— a non-volatile variable that is explicitly declared const or
constexpr and neither explicitly declared extern nor previously
declared to have external linkage; or
在 mainfile 中没有指定 size 的值,因此编译器将针对语句发出错误
int mas[size];
因为数组的大小应该是一个编译时常量表达式。
最简单的解决方案是放置常量定义
const int size = 10;
在将包含在每个翻译单元中的一些通用头文件中,其中有对常量的引用。
int mas[size];
This line has an issue. Please any guess??
正如其他用户指出的那样,问题可能是您正在尝试创建 Variable Lenght Array which is something not allowed in C++ (but almost enter in C++14 as Dynamic Arrays*).
有些编译器接受 VLA 作为扩展(非标准),所以我猜您使用的是没有此扩展或已禁用此扩展的编译器。
别担心,反正你有变通办法...
#define(不要那样做)
假设问题是VLA,如果我们确保size
作为编译时值,问题就解决了...
// file1.hpp <-- This is now a HEADER not a CPP
#define SIZE 10
// mainfile.cpp
#include "file1.hpp"
void main()
{
int mas[SIZE]; // accepted, equivalent to int mas[10].
}
constexpr
C++11 引入了 constexpr
* 关键字,可用于实现您的目标
// file1.hpp <-- This is now a HEADER not a CPP
constexpr int size() { return 10; }
// mainfile.cpp
#include "file1.hpp"
void main()
{
int mas[size()];
}
枚举
枚举是编译时常量,因此您可以这样使用它们:
// file1.hpp <-- This is now a HEADER not a CPP
enum constant { size = 10 };
// mainfile.cpp
#include "file1.hpp"
void main()
{
int mas[constant::size];
}
*如果有人找到更好的link,请告诉我。
我对外部变量和数组声明有疑问。 如何使用不在可声明文件中的全局变量声明数组。
file1.cpp
const int size = 10;
mainfile.cpp
extern const int size;
void main()
{
int mas[size];
}
int mas[size];
这行有问题。 请大家猜一猜??
你不能。数组大小必须是常量表达式;如果它是一个变量,那么该变量必须是 const
并在同一个翻译单元中初始化,以便它的值可用作常量。
如果您想在多个翻译单元之间共享值,请在 header 中定义并包含它。
C++ 不允许在运行时指定数组的大小。在您的示例中,它当然是在 link 时间指定的,但这对编译器没有帮助。 但是,如果您使用的是 C++14 编译器,并且在某些其他编译器(例如 gcc)上,您可以这样做,但它的可移植性不如动态分配内存,而且比 std::vector<> 更不方便.
供参考:https://isocpp.org/wiki/faq/freestore-mgmt#dynamic-array-len
首先常量是有内部链接的。因此这些声明
file1.cpp
const int size = 10;
和
mainfile.cpp
extern const int size;
指的是不同的实体。
在file1.cpp中声明的常量在其对应的编译单元之外是不可见的。
根据 C++ 标准(3.5 程序和链接)
3 A name having namespace scope (3.3.6) has internal linkage if it is the name of
— a non-volatile variable that is explicitly declared const or constexpr and neither explicitly declared extern nor previously declared to have external linkage; or
在 mainfile 中没有指定 size 的值,因此编译器将针对语句发出错误
int mas[size];
因为数组的大小应该是一个编译时常量表达式。
最简单的解决方案是放置常量定义
const int size = 10;
在将包含在每个翻译单元中的一些通用头文件中,其中有对常量的引用。
int mas[size];
This line has an issue. Please any guess??
正如其他用户指出的那样,问题可能是您正在尝试创建 Variable Lenght Array which is something not allowed in C++ (but almost enter in C++14 as Dynamic Arrays*).
有些编译器接受 VLA 作为扩展(非标准),所以我猜您使用的是没有此扩展或已禁用此扩展的编译器。
别担心,反正你有变通办法...
#define(不要那样做)
假设问题是VLA,如果我们确保size
作为编译时值,问题就解决了...
// file1.hpp <-- This is now a HEADER not a CPP
#define SIZE 10
// mainfile.cpp
#include "file1.hpp"
void main()
{
int mas[SIZE]; // accepted, equivalent to int mas[10].
}
constexpr
C++11 引入了 constexpr
* 关键字,可用于实现您的目标
// file1.hpp <-- This is now a HEADER not a CPP
constexpr int size() { return 10; }
// mainfile.cpp
#include "file1.hpp"
void main()
{
int mas[size()];
}
枚举
枚举是编译时常量,因此您可以这样使用它们:
// file1.hpp <-- This is now a HEADER not a CPP
enum constant { size = 10 };
// mainfile.cpp
#include "file1.hpp"
void main()
{
int mas[constant::size];
}
*如果有人找到更好的link,请告诉我。