making variables available in the #include(d) file

I am writing a program that most of the functionality is in a .cpp file. I put some functionality in a .h file which I #include(d) at the beginning of the .cpp file.
In the .cpp file, I have an area that I refer to as the 'data segment'* where I put variables that I want visible and available everywhere. This 'data segment' is not inside any class definition or function.
As it turns out, one of the functions in the .h file needs to view and sometimes change some of those values in the 'data segment.' Unfortunately, the compiler cannot find these variables. All it says is that they are not declared in this scope. If they were part of a class, I could specify class::variable but they're not. (Maybe that's my answer?!)
I need a way to make these values visible to the function in the .h file so I can use them. Thanks in advance for help.



*Yes, data segment is a throwback to when I used to write assembly language.
I think you're talking about global variables. There is the extern keyword that is like a prototype for variables.
 
extern int dataSegVar;


But why would functions need to be able to modify variables from the header file? Are your functions templates? If so, you can cheat a little by saving the definitions in a non .h/.cpp file. The convention is the .inl extension. Then, you include the inline file from the header file.

Are there alternatives you can use without resorting to global variables?
Last edited on
Give external linkage to those variable that are needed to be accessed from outside.

For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
///////////////  implementation.cpp /////////////////

#include "header.h"

// variables with internal linkage that are programmatically
// not visible outside this implementation file (.cpp)
// these can be accessed only from within this cpp file
// ideally, most of the variables would be of this kind
namespace
{
    int a = 0 ;
    double b = 78.4 ;
    // ...
}

// variables that are programmatically visible outside this
// implementation file (.cpp) as constants
// these can be modified only from within this cpp file
// but can be inspected from outside
namespace // define them in the cpp file with internal linkage
{
    int c = 8 ;
    double d = 1.23 ;
    // ...
}

// create aliases with external linkage that can be
// accessed (but not modified) from outside
namespace data
{
    const int& cc = c ;
    const double& dd = d ;
    // ...
}

// variables that are programmatically visible outside this
// implementation file (.cpp) and can be modified from outside
// ideally, there would not be many variables of this kind
namespace data // define them in the cpp file with external linkage
{
    int e = 12 ;
    double f = 10 ;
    // ...
}

// variables which are constants known at compile-time
// define these in the header


// typical functions in the cpp file
namespace data
{
    // foo can be called from outside
    double foo() { return b += h ; } 

    // bar is not programmatically visible outside
    static double bar()
    {
        a *= g ;
        return a + b ;
    }
}


This is what the corresponding header would look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
///////////////  header.h /////////////////

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED

// declare variables that can be inspected,
// but not modified from outside
namespace data
{
    extern const int& cc ;
    extern const double& dd ;
    // ...
}

// declare variables that can be modified from outside
namespace data
{
    extern int e ;
    extern double f ;
    // ...
}

// define variables which are constants known at compile-time
namespace data
{
    constexpr int g = 100 ;
    constexpr double h  = 22.7 ;
    // ...
}

// declare functions with external linkage in the cpp file
namespace data
{
    double foo() ;
    // ...
}

// define functions with internal linkage in the header file
namespace data
{
    inline double baz()
    {
        ++e ;
        return cc + dd + e + f + g + h ;
    }
}

#endif // HEADER_H_INCLUDED 


Typical use from another file:

1
2
3
4
5
6
7
8
9
10
///////////////  main.cpp /////////////////

#include "header.h"

int main()
{
    data::foo() ;
    data::baz() ;
    return data::g ;
}
Topic archived. No new replies allowed.