简体   繁体   English

如何使用向量成员外部构造结构?

[英]How to extern const struct with vector members?

I am splitting my code into declarations and definitions.我将我的代码拆分为声明和定义。 I didn't get any problems until trying to do anything with this constant struct, consisting of vectors.在尝试使用这个由向量组成的常量结构做任何事情之前,我没有遇到任何问题。 Leaving this code in header leads to multiple definitions type of errors.将此代码留在 header 中会导致多种定义类型的错误。

// Core.h:
const struct ConstData {
    vector<int> numbers1 = { 1, 2, 3, 4, 5 };
    vector<int> numbers2 = { 0, 10, 20, 30 };
} Constants;

I tried moving this code into cpp file and using extern with the struct in the header, but that didn't help.我尝试将这段代码移动到 cpp 文件中,并在 header 中将extern与结构一起使用,但这没有帮助。 On use cases of struct fields in other files I was getting undeclared identifiers type of errors.在其他文件中结构字段的用例中,我遇到了未声明的标识符类型的错误。

// Core.cpp:
const struct ConstData {
    vector<int> numbers1 = { 1, 2, 3, 4, 5 };
    vector<int> numbers2 = { 0, 10, 20, 30 };
} Constants;
// Core.h:
extern const struct ConstData Constants;

Tried putting the struct, with uninitialised fields, before the extern .尝试将带有未初始化字段的结构放在extern之前。 Thought that might help, so compiler sees with what type of struct it's working and what fields it has.认为这可能会有所帮助,因此编译器会查看它正在使用哪种类型的结构以及它具有哪些字段。 But that is considered redefinition, since I have same struct in cpp file.但这被认为是重新定义,因为我在 cpp 文件中有相同的结构。

// Core.h:
const struct ConstData {
    vector<int> numbers1;
    vector<int> numbers2;
};

extern const struct ConstData Constants;
// Core.cpp:
const struct ConstData {
    vector<int> numbers1 = { 1, 2, 3, 4, 5 };
    vector<int> numbers2 = { 0, 10, 20, 30 };
} Constants;

I'm kinda stuck at this point.我有点被困在这一点上。 Looking up how people are dealing with this issue didn't lead me to much success.查看人们如何处理这个问题并没有让我取得很大的成功。 In extern docs of Microsoft was said that const modifier changes linkage type (internal or external).在 Microsoft 的extern文档中, const修饰符会更改链接类型(内部或外部)。 So I tried all of the above again, but playing with the const at the same time — no progress on that.所以我再次尝试了上述所有方法,但同时使用了const——没有任何进展。 Maybe I missed something..也许我错过了什么..

Hope I have supplied enough info and that community will be able to help me!希望我提供了足够的信息,希望社区能够帮助我!

This code这段代码

const struct ConstData {
    vector<int> numbers1 = { 1, 2, 3, 4, 5 };
    vector<int> numbers2 = { 0, 10, 20, 30 };
} Constants;

...declares both the structure type ConstData and the variable Constants . ...声明结构类型ConstData和变量Constants You cannot re-declare the structure in the .cpp file if you already declare it in the header file.如果您已经在 header 文件中声明了结构,则不能在.cpp文件中重新声明该结构。

You want to split both declarations in the header and only initialize the variable in the .cpp file:您想要拆分 header 中的两个声明,并且只初始化.cpp文件中的变量:

// Core.h
#include <vector>

struct ConstData {
    std::vector<int> numbers1;
    std::vector<int> numbers2;
};

extern const ConstData Constants;

// Core.cpp
#include <Core.h>

const ConstData Constants{
    { 1, 2, 3, 4, 5 },
    { 0, 10, 20, 30 }
};

I would recommend a struct with static members and std::array for the elements.我会推荐一个具有 static 个成员和std::array元素的结构。 Since c++17 you can declare this in header file:由于 c++17 您可以在 header 文件中声明:

#include <array>
struct ConstData {
    static constexpr std::array numbers1 = { 1, 2, 3, 4, 5 };
    static constexpr std::array numbers2 = { 0, 10, 20, 30 };
};

And use ConstData::number1 / ConstData::number2 .并使用ConstData::number1 / ConstData::number2

Before c++17 it is a bit more effort:c++17之前需要付出更多的努力:

// Header file:
struct ConstData {
    static constexpr std::array<int, 5> numbers1 = { 1, 2, 3, 4, 5 };
    static constexpr std::array<int, 5> numbers2 = { 0, 10, 20, 30 };
};

// c++-File
constexpr std::array<int,5> ConstData::numbers1;
constexpr std::array<int,5> ConstData::numbers2;

These are compile-time structures.这些是编译时结构。 So they appear 1:1 in memory. Using std::vector always allocates memory and copies the elements at runtime.因此它们在 memory 中以 1:1 的比例出现。使用std::vector始终分配 memory 并在运行时复制元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM