Create struct in header file and reuse it.

Hey,

I'm writing a program which contains this map:

1
2
3
4
5
6
7
  
    const fifo_map<std::string, std::vector<int>> chords {
        {"maj", {0,4,7}},
        {"min", {0,3,7}},
        {"aug", {0,4,8}},
}


I have a few of them and I want to put them in a header file for reusal and better visability.

What would be the best way to achieve this? How do I declare the header file and how to I get the values in my original file?
A header file is used just to tell the compiler

  • what things look like (types)
  • what things exist elsewhere (values and functions)

To do so with a value, use the extern keyword.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// example.hpp
#ifndef EXAMPLE_HPP
#define EXAMPLE_HPP

#include <string>
#include <vector>

namespace example
{

        extern const fifo_map<std::string, std::vector<int>> chords;

}

#endif 

You must, of course, actually define the object somewhere:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// example.cpp

#include "example.hpp"

namespace example
{

        const fifo_map<std::string, std::vector<int>> chords {
                {"maj", {0,4,7}},
                {"min", {0,3,7}},
                {"aug", {0,4,8}},
        };

}

Compile and link example.cpp with the rest of your program.
That’s it!

Hope this helps.
Last edited on
In the specific case of const variables in the global or namespace scope, you can just directly declare the object like how it is in the original post in the header file, because the const variables will implicitly have internal linkage.

But the above way is more general and will work with const and non-const objects alike.
Last edited on
Wait until C++20 modules become universally supported, the need for separate headers and compilation units will meld into the background.
From C++17 there is inline which makes this easier for non-const variables:

An inline function or variable (since C++17) with external linkage (e.g. not declared static) has the following additional properties:

There may be more than one definition of an inline function or variable (since C++17) in the program as long as each definition appears in a different translation unit and (for non-static inline functions and variables (since C++17)) all definitions are identical. For example, an inline function or an inline variable (since C++17) may be defined in a header file that is #include'd in multiple source files.


https://en.cppreference.com/w/cpp/language/inline
Thank you all already for the answers, is there also a way to store them in struct just like this:
{"maj", {0,4,7}},
So without declaring a map? Cause maybe in the future i want to use them in another way.
(a bit the idea of an xml file).
For example, i got the example from here:https://github.com/BespokeSynth/BespokeSynth/blob/main/resource/userdata_original/scales.json

They use a json file, is this usable in c++?
Last edited on
json, xml are special text file formats. C++ can use them, but not 'directly' or internally as part of the code: you have to extract them (when reading a file, or similar) into data structures (like a map, or a struct, or vector etc) to really make use of them for anything nontrivial.

you can certainly store them in a struct.
consider?
1
2
3
4
5
6
7
8
enum chordtype{maj,min,aug,aug7th, ..?.., max_chordtype};
string chordtypenames[max_chordtype]{"maj","min", ... };
struct chord 
{
    chordtype type; //you can keep this as string if you want, the enum/string pair idea 
//may be more flexible: its just an example of an alternate way to store it (its like a map)
    int numbersofsomeuncleartype[3]; //this needs a meaningful name
};

and then you can make a container of the above as a vector, map, tree, whatever you want...

You can also use an actual json-parsing library, but this still eventually turns the json into maps/vectors.
https://github.com/nlohmann/json
ok thank you all, i understand now, someone else also pointed me to nlohmann, i also use a fifo_map made by him, very cool stuff!
Topic archived. No new replies allowed.