Subnetting Program -- [variable] was not declared in this scope"

I"m writing a subnetting program, and I'm having an issue with scope and inheritance. Basically, I've created 4 integer arrays.

I want these integer arrays to be initialized in one for-loop, and then be printed afterwards in a separate for-loop.

Therefore, I declared them inside an "If-block", but outside of any for-loop, because I need the arrays to persist. However, when I reach the for-loop that prints to standard output, my compiler says they were not declared in that scope.

And not only that. When I reach my final for-loop inside this If-block, my compiler fails to recognize a struct object I'd created at that point as well.

So, I'll post my code in two segments. The first half shows how I declared, allocated, and initialized my arrays:
--- the First Segment ---
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
  // A Class A Subnet Mask will have a CIDR of 8-15 bits.
  // Only Class A Subnet Masks will have access to Class A addresses.
  if( ipData.CIDR >= 8 && ipData.CIDR <= 15 )
  {
     // This int limits the number of rows I print in final output
     int ctrlVar = 0;
		
     // I need to declare these arrays, and have their size be inherited in all 3 If-Cases below:
     int *validSubnets = new int[ ipData.numSubnets ];
     int *broadcastAddrs = new int[ ipData.numSubnets ];
		
     int *firstHost = new int[ ipData.numSubnets ];
     int *lastHost = new int[ ipData.numSubnets ];
		
     // Secondly, we calculate the block size of the subnet mask.
     ipData.blockSize = 256 - ipData.ipToken2;
		
     for( int i = 0; i < ipData.ipToken2; i += ipData.blockSize)
     {
          validSubnets[ctrlVar] = i;
          ctrlVar++;
     }
		
     // Calculating the Broadcast Addresses. Resetting control var.
     ctrlVar = 0;
     for( int i = 0; i < ipData.ipToken2; i += ipData.blockSize )
     {
          if( i == ipData.ipToken2 )
          {
               broadcastAddrs[ctrlVar] = 255;
          }
          else
          {
               broadcastAddrs[ctrlVar] = validSubnets[i+1] - 1;
          }

          ctrlVar++;
     }
		
     // Now I calculate the first hosts of the valid hosts arrays.
     //
     // Note -- Integer i's bounds will need to be adjustd for these 
     //           next two For-Loops between IP Classes.
     for(int i = 16; i < 24; i++)
     {
          firstHost[i] = validSubnets[i] + 1;
     }
		
     // Now I calculate the last hosts of the valid hosts arrays.
     for(int i = 16; i < 24; i++)
     {
          lastHost[i] = broadcastAddrs[i] - 1;
     }


In this Second Segment, the arrayes declared above should be inherited by the For-Loop below, but they aren't.
I have an ipTable struct object declared above my main() method that works until this segment.

The compiler says my ipData object, and my four arrays below, as well as my int index 'class_c_second_index' all
"were not declared in this scope".

--- The Second Segment ---

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
     /************************************************************
          --- Now I print out the Subnet Table ---
     ************************************************************/
     cout << left << setw(16) << "Subnet " << left << setw(16) << "First Host ";
     cout << left << setw(16) << "Last Host " << left << setw(16) << "Broadcast ";
		
     // At each-and-every iteration of this for loop, an element is printeed from 
     //  each of the 4 arrays. And 8 rows are printed, spanning 4 columns.
		
     // This is named 'second index' in the sense that it's the second control variable I'm instituting
     //  to controla for-loop to limit the number of elements of an array that are printed per table.
     int class_c_second_index = 0;
     
     for( int i = 8; i < 16; i++)
     {
          cout << ipData.ipToken1 << "." << validSubnets[i] << "." << ipData.ipToken3 << "." << ipData.ipToken4 << " ";
          cout << ipData.ipToken1 << "." << firstHost[i] <<  "." << ipData.ipToken3 << "." << ipData.ipToken4 << " ";
          cout << ipData.ipToken1 << "." << lastHost[i] <<  "." << ipData.ipToken3 << "." << ipData.ipToken4 << " ";
          cout << ipData.ipToken1 << "." << BroadcastAddrs[i] <<  "." << ipData.ipToken3 << "." << ipData.ipToken4 << "\n";
			
          // When this reaches 8 items, it's the end of the table, and a new one must be initiated 
          // --- I.E. The Class B and Class C stuff below this block. 
          // ----- (Atm -- Lines 321 && 396 denote the other 2/3 of this data)
          if( class_c_second_index == 7 )
          {
               cout << "\n\n";
          }
          else
          {
               class_c_second_index++;
          }
     }
		
     // And then delete the Arrays...
     delete[] validSubnets;
     delete[] broadcastAddrs;
     delete[] firstHost;
     delete[] lastHost;

  // End of the If block of code that started in segment 1
  }
And if I may, I'd like to provide my error log below.

I'd like to mention that I've shared my code for my Class A subnetting block, and there are a Class B and Class C that do the same things. Create the same variables and etc.

The distinction is, that each subnet class if-block deals with a different set of subnets.

Ultimately, that's why there are so many errors. If you can help me the scope of my class A code block above, I will be able to fix the other two as well. Right now, however, I'm in the dark about why my inheritance/scope isn't working.

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
newLab2.cpp: In function 'int main()':
newLab2.cpp:325:38: error: 'BroadcastAddrs' was not declared in this scope
    cout << ipData.ipToken1 << "." << BroadcastAddrs[i] <<  "." << ipData.ipToken3 << "." << ipData.ipToken4 << "\n";
                                      ^
newLab2.cpp:395:18: error: 'broadCastAddrs' was not declared in this scope
    lastHost[i] = broadCastAddrs[i] - 1;
                  ^
newLab2.cpp:415:64: error: 'BroadcastAddrs' was not declared in this scope
    cout << ipData.ipToken1 << "." << ipData.ipToken2 << "." << BroadcastAddrs[i] <<  "." << ipData.ipToken4 << "\n";
                                                                ^
newLab2.cpp:482:18: error: 'broadCastAddrs' was not declared in this scope
    lastHost[i] = broadCastAddrs[i] - 1;
                  ^
newLab2.cpp:496:3: error: expected ';' before 'class_c_second_index'
   class_c_second_index = 0;
   ^
newLab2.cpp:502:90: error: 'BroadcastAddrs' was not declared in this scope
    cout << ipData.ipToken1 << "." << ipData.ipToken2 << "." << ipData.ipToken3 << "." << BroadcastAddrs[i] << "\n";
                                                                                          ^
newLab2.cpp:504:8: error: 'class_c_second_index' was not declared in this scope
    if( class_c_second_index == 7 )
        ^
newLab2.cpp: In function 'void subnet_routing(ipTable&)':
newLab2.cpp:670:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 0 && ipData.subNet3 == 0 && ipData.subNet4 == 0 )
      ^
newLab2.cpp:674:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 128 && ipData.subNet3 == 0 && ipData.subNet4 == 0 )
      ^
newLab2.cpp:678:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 192 && ipData.subNet3 == 0 && ipData.subNet4 == 0 )
      ^
newLab2.cpp:682:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 224 && ipData.subNet3 == 0 && ipData.subNet4 == 0 )
      ^
newLab2.cpp:686:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 240 && ipData.subNet3 == 0 && ipData.subNet4 == 0 )
      ^
newLab2.cpp:690:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 248 && ipData.subNet3 == 0 && ipData.subNet4 == 0 )
      ^
newLab2.cpp:694:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 252 && ipData.subNet3 == 0 && ipData.subNet4 == 0 )
      ^
newLab2.cpp:698:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 254 && ipData.subNet3 == 0 && ipData.subNet4 == 0 )
      ^
newLab2.cpp:702:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 255 && ipData.subNet3 == 0 && ipData.subNet4 == 0 )
      ^
newLab2.cpp:706:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 255 && ipData.subNet3 == 128 && ipData.subNet4 == 0 )
      ^
newLab2.cpp:710:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 255 && ipData.subNet3 == 192 && ipData.subNet4 == 0 )
      ^
newLab2.cpp:714:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 255 && ipData.subNet3 == 224 && ipData.subNet4 == 0 )
      ^
newLab2.cpp:718:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 255 && ipData.subNet3 == 240 && ipData.subNet4 == 0 )
      ^
newLab2.cpp:722:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 255 && ipData.subNet3 == 248 && ipData.subNet4 == 0 )
      ^
newLab2.cpp:726:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 255 && ipData.subNet3 == 252 && ipData.subNet4 == 0 )
      ^
newLab2.cpp:730:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 255 && ipData.subNet3 == 254 && ipData.subNet4 == 0 )
      ^
newLab2.cpp:734:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 255 && ipData.subNet3 == 255 && ipData.subNet4 == 0 )
      ^
newLab2.cpp:738:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 255 && ipData.subNet3 == 255 && ipData.subNet4 == 128 )
      ^
newLab2.cpp:742:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 255 && ipData.subNet3 == 255 && ipData.subNet4 == 192 )
      ^
newLab2.cpp:746:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 255 && ipData.subNet3 == 255 && ipData.subNet4 == 224 )
      ^
newLab2.cpp:750:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 255 && ipData.subNet3 == 255 && ipData.subNet4 == 240 )
      ^
newLab2.cpp:754:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 255 && ipData.subNet3 == 255 && ipData.subNet4 == 248 )
      ^
newLab2.cpp:758:6: error: 'ipData' was not declared in this scope
  if( ipData.subNet2 == 255 && ipData.subNet3 == 255 && ipData.subNet4 == 252 )
      ^
Last edited on
> newLab2.cpp:758
Lemme guess, you have an 800 line main() function, and with that complete lack of structure, you've no actual idea what the scope of anything actually is.

Start splitting the code up into functions you can analyse properly.

BroadcastAddrs, broadCastAddrs, and broadcastAddrs
are three separate identifiers. Choose a spelling and stick with it.

The problem with class_c_second_index is most likely a consequence of the cause of this error message:
newLab2.cpp:496:3: error: expected ';' before 'class_c_second_index'
Which does not necessarily mean you've missed a semicolon. Sometimes, this just means the compiler's parser is confused because of a different, misdiagnosed problem earlier in the code.
God I hate spelling errors. Thank you for the help.

It's not that I couldn't decide how to spell it. I'd just written a lot of code, in a short amount of time, and at a very quick typing speed. Made a day of it in fact, 7 AM - 7 PM, approximately, lol.


I've managed to boil all the errors down to the bottom ones.
Which is to say, that my main() method isn't recognizing 'ipData' as a valid data type, even though an object of that type was declared, and the struct body is defined. :\

---------------------------------------------------------------------------------------------------------------------------------
Side Note: As for why most of this code is in Main(). The variables I'm dealing with in this segment are int arrays. It's easy for me to create a struct or class, stick a bunch of integers, strings, and etc in its definition, and then pass around a single object of that data type, and modify its members in void functions.

I had mixed results a few days ago when I was trying to pass around int/char arrays. I had trouble making the elements of each array persist, and didn't want to make a pointer-to-pointers, and add that to the struct. I hate passing those around in function calls, and parameter lists..
Last edited on
Since this is a C++ program you don't have to use char arrays or pointers.
You can use string and vectors. They are easier to use and a bit safer.
Notes:
IPv4 address is a 32-bit unsigned integer. One number. The dot-decimal notation is just for humans. https://en.wikipedia.org/wiki/Dot-decimal_notation

Classless addressing was introduced already 1993 to replace the classful (class A, B, C)
https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#CIDR_notation

One can calculate subnet, broadcast address, and netmask from two numbers: address and prefix.
There are no ifs about it.
Topic archived. No new replies allowed.