简体   繁体   中英

I'm having some scope issue (unidentified identifier error after a for loop)

I'm using visual studio 2008 to do some problems and brusg up on using c++. I have an error and I don't know why it occurs. Here's all the code. The error occurs on the line :

cout<<levels[0][0]->left->value;

with error:

error C2065: 'levels' : undeclared identifier

a shorthand of what's happening to levels is this:

//declaring it
binaryValNode*** levels;
levels = new binaryValNode** [size];

//adding arrays to the array:
 for(int i = 0;i<size;i++){
     levels[i] = new binaryValNode* [size];
//adding the objects
 for(int k = 0; k <= i ; k++)
 {
     levels[i][k] = new binaryValNode();
 }
//I tested cout here and it works fine
}
//but loses scope here(?)

binaryValNode is a struct with int value,binaryValNode* left and binaryValNode* right.

thanks!

code:

#include <iostream>
#include <fstream>
#include "binaryValNode.h"

using namespace std;

int main() {
 int length = 0;
 int size = 0;
 ifstream myReadFile;
 myReadFile.open("input.txt");
 char* c = new char[3];
 if (myReadFile.is_open()) {
 while (myReadFile.getline(c,(size+1)*3)) {
     size++;
     c = new char[(size+1)*3];
 }

 binaryValNode*** levels;
 levels = new binaryValNode** [size];

 myReadFile.clear();
 myReadFile.seekg(0);
 for(int i = 0;i<size;i++){
     levels[i] = new binaryValNode* [size];

     c = new char[(i+1)*3];
     myReadFile.getline(c,(i+1)*3);

     for(int k = 0; k <= i ; k++)
     {
         levels[i][k] = new binaryValNode();
         if(c[3*k] != '0')
         {
             levels[i][k]->value = ((int) c[(3*k)+1]-48) + 10*((int) c[(3*k)]-48);
         }
         else
         {
            levels[i][k]->value = (int) c[(3*k)+1]-48;
         }

         //
         if(i!=0){
             if(k==0){//only left parent
                levels[i-1][k]->left = levels[i][k];
             }
             else if(k==i){//only right parent
                 levels[i-1][k-1]->right = levels[i][k];
             }
             else{
                levels[i-1][k]->left = levels[i][k];
                levels[i-1][k-1]->right = levels[i][k];
             }
         }
     }
 }
}

myReadFile.close();
cout<<levels[0][0]->left->value;
cin.get();
return 0;
}

Fix your indentation (for example, gg=G in Vim).

Right now you have

int main() {
    // ...
    if (myReadFile.is_open()) {
        // ...
        binaryValNode*** levels;
        // ...
    }
    // ...
    cout << levels[0][0]->left->value;
    // ...
}

where levels is very clearly out of scope.

Sort out your indentation - it will show that you have one too may closing }, and hence the problem line occurs after the end of main().

Try edit/advanced/format-selection

这需要在条件之前移动:

    binaryValNode*** levels;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM