Home > Mobile >  How to extern const struct with vector members?
How to extern const struct with vector members?

Time:01-03

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.

// 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. 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. 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.

// 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). So I tried all of the above again, but playing with the const at the same time — no progress on that. Maybe I missed something..

Hope I have supplied enough info and that community will be able to help me!

CodePudding user response:

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. You cannot re-declare the structure in the .cpp file if you already declare it in the header file.

You want to split both declarations in the header and only initialize the variable in the .cpp file:

// 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 }
};

CodePudding user response:

I would recommend a struct with static members and std::array for the elements. Since c 17 you can declare this in header file:

#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.

Before c 17 it is a bit more effort:

// 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.

  • Related