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?

5
  • Templates need to be seen in every compilation unit, hence there's no way to do in outside of a header file. Commented Oct 17, 2016 at 11:40
  • 2
    This might be part of the problem. Commented Oct 17, 2016 at 11:40
  • This has been asked Commented Oct 17, 2016 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? Commented Oct 17, 2016 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.) Commented Oct 17, 2016 at 11:52

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

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
@MenasheRosemberg You do have the global array arr. In this example arr is not duplicated.
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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.