My goal is that when I call myFunction from within main, I do not have to pass the SIZE constant. The function within myFunction.cpp should then run and simply output the contents of the array.
I've read some notes on external linkage but I feel like I don't understand it well enough to apply anything in this case. Ideally main.cpp shouldn't be changed much other than giving the namespace including SIZE a name if absolutely necessary.
main.cpp
#include <iostream>
#include "myFunction.hpp"
namespace
{
extern const int SIZE = 10;
}
void myFunction(char []);
int main()
{
char myArray[SIZE] = "123456789";
myFunction(myArray);
}
myFunction.h
#ifndef MYFUNCTION_H_INCLUDED
#define MYFUNCTION_H_INCLUDED
namespace
{
extern const int SIZE;
}
#endif // MYFUNCTION_H_INCLUDED
myFunction.cpp
#include <iostream>
void myFunction(char myArray[])
{
for(int i = 0; i < SIZE; ++i)
{
std::cout << myArray[i] << " ";
}
}
With what I have so far I still get the error that SIZE was not declared in that scope (myFunction.cpp).
Question: What do I have to do to make this example work? If possible, I'd also love an explanantion of why one would handle sharing SIZE this way.