有谁能告诉我如何解决以下问题吗?
clang++-7 -pthread -std=c++17 -o main createLibrary/configuration.cpp createLibrary/growbox.cpp createLibrary/helper.cpp createLibrary/httprequests.cpp main.cpp
In file included from createLibrary/configuration.cpp:2:
In file included from createLibrary/configuration.h:1:
In file included from createLibrary/growbox.h:12:
createLibrary/httprequests.h:13:10: error: fields must have a constant size:
'variable length array in structure' extension will never be supported
char device[configuration::maxNameSize];
^
1 error generated.我将.h文件按order configuration.h,httprequests.h包括在内。我希望在configuration.cpp文件中配置所有必要的配置参数,但是我得到了显示的错误。我在这里做错什么了?
configuration.h
extern int const maxNameSize;configuration.cpp
int const configuration::maxNameSize = 30;httprequests.h
char device[configuration::maxNameSize];httprequests.cpp
char HTTPREQUESTS::device[configuration::maxNameSize];发布于 2020-05-04 09:48:21
像这样声明maxNameSize
// configuration.h
class configuration
{
public:
static const int maxNameSize = 30;
...
};不需要在configuration.cpp中定义它。
您的方法并不使maxNamesize成为编译时间常数。
编辑,我假设configuration是一个类。如果它是一个名称空间,那么执行下面的操作
// configuration.h
namespace configuration
{
const int maxNamesize = 30;
...
}常量是一个定义规则的例外,所以可以在头文件中定义它们。
发布于 2020-05-04 09:50:13
extern const int不是常量表达式。
变量在点P处的常量表达式中可用,条件是
(强调后加)
我希望在configuration.cpp文件中配置所有必要的配置参数。
你运气不好。maxNameSize的值必须对其编译时用户可见。
发布于 2020-05-04 09:54:14
我在这里做错什么了?
您已经定义了一个大小不是编译时间常数的数组变量。
解决方案:你可以
std::vector。https://stackoverflow.com/questions/61589249
复制相似问题