4

I created a code file to keep all my global variables and one of them is an array like this one:

global.cpp

#include <array>

array<string, 3> arr = {"value1", "value2","value3"};

I test arrays values in another code file like this:

testarrays.cpp

#include <iostream>
#include <array>

template <size_t N>
void TestingArrays(const array<string, N>& ArrT);

void ArrayTester()
{
     extern array<string,3> arr;

     array <string, 2> localarr = {"v1" ,"v2"};

     TestingArrays(localarr);
     TestingArrays(arr);
}

template <size_t N>
void TestingArrays(const array<string, N>& ArrT) 
{
     for (auto it = ArrT.cbegin(); it != ArrT.cend(); ++it)
     {
         cout << "Testing " << *it << endl;
     }
}

Everything is beautiful except for one thing. I use this global array (Arr) in many other places.

That means if I need to change the number of variables (it have 3 in this example) I need to do the same over all code files... ...crazy...

I thought to use a template like this:

testarrays.cpp

...
     template <size_t N>
     extern array<string,N> arr;
...

...but it didn't compiled.

Someone have a tip to solve this problem?

TheArchitect
  • 1,160
  • 4
  • 15
  • 26
  • Templates need to be seen in every compilation unit, hence there's no way to do in outside of a header file. – πάντα ῥεῖ Oct 17 '16 at 11:40
  • 2
    [This](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) might be part of the problem. – NathanOliver Oct 17 '16 at 11:40
  • This has been asked – amanuel2 Oct 17 '16 at 11:41
  • Declare the arrays in a header file that is included by all source files? It's not really clear exactly what your problem actually is. Why do you "need to change the number of variables"? Can't you use e.g. a vector of arrays? – Some programmer dude Oct 17 '16 at 11:42
  • 6
    The "normal" way is to have `extern` declarations in headers. (The phrase "all my global variables" suggests that a redesign might be in order, though.) – molbdnilo Oct 17 '16 at 11:52

2 Answers2

2

A using statement could make this easier. Then you could use myArr in multiple places but only change the size once:

//header.h
#include <array>
#include <string>

using myArr = std::array<std::string, 3>;

extern myArr arr;

Next put the definition of the global in a new file:

//myarr.cpp
#include "header.h"

myArr arr = {"value1", "value2","value3"};

Finally use it in the other compilation units (.cpp files):

//main.cpp
#include "header.h"

#include <iostream>
#include <array>

template <size_t N>
void TestingArrays(const std::array<std::string, N>& ArrT);

void ArrayTester()
{
    //extern array<string, 3> arr; // global var doesn't need to be declared here if the header is included

    std::array<std::string, 2> localarr = {"v1" ,"v2"};

    TestingArrays(localarr);
    TestingArrays(arr);
}

template <size_t N>
void TestingArrays(const std::array<std::string, N>& ArrT)
{
    for(auto it = ArrT.cbegin(); it != ArrT.cend(); ++it)
    {
        std::cout << "Testing " << *it << std::endl;
    }
}

int main() {
    auto value = arr[1];
    ArrayTester();
    return 0;
}
wally
  • 10,717
  • 5
  • 39
  • 72
  • That is a solution, but it means I won't have the global array and I will construct this array when I call a functionduplicated arrays in memory always I call a function – TheArchitect Oct 24 '16 at 10:00
  • @MenasheRosemberg You do have the global array `arr`. In this example `arr` is not duplicated. – wally Oct 24 '16 at 12:18
1

Just make header file with the forward declaration:

#pragma once
#include <array>
#include <string>

extern std::array<std::string, 3> arr;

and include it where you need:

#include"arr.h"
...
void ArrayTester()
{
     array <string, 2> localarr = {"v1" ,"v2"};

     TestingArrays(localarr);
     TestingArrays(arr);
}
Victor Dyachenko
  • 1,363
  • 8
  • 18