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