Error related to scope of variables

Hello everyone. I am new to this forum.

I have recently tried to break up one of my programs into several smaller files in order to make it more easily readable and editable. To do this, I added a number of custom header files and source files, and divided a lot of the code I previously had directly in main() into several smaller functions which were placed in those new files.

My program compiled fine in it's previous version, but is now returning the following compiler error for the majority of my variables: "not declared in this scope."

I am unsure what I am doing wrong, but I'm apparently misusing my variables' defined scope. Here's a small program I wrote to show the problem. It's just a program taking an array of integers, using a function to set the number "5" to everyone of the array's elements, and then printing the array out with a printf.

Below is the main source file, with the main() function in it:

samplemain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include "funcheader.h"

int testarray [6] = {10,20,30,40,50,60};

int main()
{

testfunction();

int k;
for(k=0; k<=5; k++)
{printf("\n%d",testarray[k]);}

 return 0;
}



Here's the funcheader.h header file, where I define the function:

funcheader.h
1
2
3
4
#include <stdio.h>

void testfunction();



And here's the source file where the function itself is to be found:

function.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "funcheader.h"

void testfunction()
{

int i;

    for(i=0;i<=5;i++)
    {
        testarray[i]=5;
    }

}



The array itself (called "testarray") is defined as a global variable, so it should be accessible from anywhere without distinctions. However, the compiler returns me the error:

In function 'void testfunction()':
10: error: 'testarray' was not declared in this scope



Any help or feedback as to what I'm doing wrong in terms of variable scope would be most appreciated. Thank you in advance for any reply.

Regards,

Okaya



Neither function.cpp nor funcheader.h declare a variable named "testarray". It is declared
in main.cpp.
To elaborate a bit more on what jsmith said

An Article I wrote wrote:
C++ programs are built in a two stage process. First, each source file is compiled on its own. The compiler generates intermediate files for each compiled source file. These intermediate files are often called object files -- but they are not to be confused with objects in your code. Once all the files have been individually compiled, it then links all the object files together, which generates the final binary (the program).

This means that each source file is compiled separately from other source files. As a result of this, in terms of compiling, "a.cpp" is clueless as to what's going on inside of "b.cpp".


Basically, 'testarray' is not defined in function.cpp, therefore when the compiler compiles function.cpp it doesn't know wtf you're talking about. The compiler doesn't see testmain.cpp when it compiles function.cpp. It only compiles one cpp file at a time.

Globals are ill advised anyway. It's better to pass the array by pointer to the function:


1
2
3
4
5
6
7
8
9
10
11
12
// in function.cpp
void testfunction(int testarray[6])  // also change the .h file appropriately
{

int i;

    for(i=0;i<=5;i++)
    {
        testarray[i]=5;
    }

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// in testmain.cpp

//int testarray [6] = {10,20,30,40,50,60};  // don't make it global

int main()
{

int testarray[6] = {10,20,30,40,50,60};

testfunction(testarray);  // pass it to the function

int k;
for(k=0; k<=5; k++)
{printf("\n%d",testarray[k]);}

 return 0;
}
jsmith, Disch, thank you very much, I appreciate your feedback on my question.

Also, Disch, thank you for taking the time to elaborate further, your comments were very helpful. And your article is most informative, I just located it on this website.

Okaya
Last edited on
Topic archived. No new replies allowed.