Home > OS >  C declaration and scope issues
C declaration and scope issues

Time:12-20

I encountered the error : error: ‘data_structure’ was not declared in this scope, when compile following codes.

I assume errors happened in :

  • root = data_structure.insert(root, data[data_ind]);
  • data_structure.insert(data[data_ind]);
  • TreapNode<int> *res = data_structure.search(root, search_data[s_data_ind]);
  • bool res = data_structure.search(search_data[s_data_ind]);

But I don't know what I should correct.

complete codes : https://gist.github.com/theabc50111/05651b8c125feaaff5f80f15deb535f4

partial codes:

void test(string file_name_it, string file_name_st, string type_record)
{
    int var_range = 30; // the range of variable in skip list 
    int min_data_qty = 10; // set the min amount of imput data
    int max_data_qty = 30; // set the max amount of imput data
    clock_t i_begin_time, i_end_time, s_begin_time, s_end_time;
    vector<double> i_time_records, s_time_records;
    vector<int> search_data = gen_rand_array(100000, var_range); // generate search data
    srand(time(NULL));

    for (int data_qty=min_data_qty; data_qty<=max_data_qty; data_qty  )
    {
        if (type_record.compare("hash table"))
        {
            Hash<int> data_structure(pow(2,data_qty));
            cout << "start test hash table insert & search\n";
        }
        else if (type_record.compare("skip list"))
        {
            SkipList<int>  data_structure;
            cout << "start test Skip List insert with probability :" <<  data_structure.p << endl;
        }
        else if (type_record.compare("sorted array"))
        {
            cout << "start test sorted array insert & search\n";
            SortedArray<int>  data_structure;
        }
        else if (type_record.compare("treap"))
        {
            cout << "start test treap insert & search\n";
            TreapNode<int> data_structure;
            TreapNode<int> *root = nullptr;
        }

        vector<int> data = gen_rand_array(pow(2,data_qty),var_range);
        i_begin_time = clock();
        for (int data_ind=0; data_ind<data.size(); data_ind  )
        {
            if (type_record.compare("treap"))
            {

                root = data_structure.insert(root, data[data_ind]);
            }
            else
            {
                data_structure.insert(data[data_ind]);
            }

        }
        i_end_time = clock();

        s_begin_time = clock();
        for (int s_data_ind=0; s_data_ind<search_data.size(); s_data_ind  )
        {
            if (type_record.compare("treap"))
            {
                TreapNode<int> *res =  data_structure.search(root, search_data[s_data_ind]);
                // (res == NULL)? cout << "Not found\n" : cout << "found\n";
            }
            else
            {
                bool res = data_structure.search(search_data[s_data_ind]);
                (res == false)? cout << search_data[s_data_ind] <<" is not found\n" : cout << search_data[s_data_ind] << " found\n";
            }
        }
        s_end_time = clock();

        double i_spend_time = (double)(i_end_time-i_begin_time) / CLOCKS_PER_SEC;
        double s_spend_time = (double)(s_end_time-s_begin_time) / CLOCKS_PER_SEC;
        cout << "K=" << data_qty << ", insert time: " << i_spend_time << ". search time: " << s_spend_time << endl;
        i_time_records.push_back(i_spend_time);
        s_time_records.push_back(s_spend_time);
        output_file(file_name_it, type_record, i_time_records);
        output_file(file_name_st, type_record, s_time_records);
        // data_structure.print();
    }

}


int main(){
    test("sa_i_time.csv", "sa_s_time.csv", "sorted array");


    return 0;
}

errors:

/home/ywt01/Documents/codes/nccu_cs_hw/DataStructure_HW/HW3/hw3.cpp: In function ‘void test(std::string, std::string, std::string)’:
/home/ywt01/Documents/codes/nccu_cs_hw/DataStructure_HW/HW3/hw3.cpp:800:17: error: ‘root’ was not declared in this scope
  800 |                 root = data_structure.insert(root, data[data_ind]);
      |                 ^~~~

/home/ywt01/Documents/codes/nccu_cs_hw/DataStructure_HW/HW3/hw3.cpp:800:24: error: ‘data_structure’ was not declared in this scope
  800 |                 root = data_structure.insert(root, data[data_ind]);
      |                        ^~~~~~~~~~~~~~

/home/ywt01/Documents/codes/nccu_cs_hw/DataStructure_HW/HW3/hw3.cpp:804:17: error: ‘data_structure’ was not declared in this scope
  804 |                 data_structure.insert(data[data_ind]);
      |                 ^~~~~~~~~~~~~~

/home/ywt01/Documents/codes/nccu_cs_hw/DataStructure_HW/HW3/hw3.cpp:815:40: error: ‘data_structure’ was not declared in this scope
  815 |                 TreapNode<int> *res =  data_structure.search(root, search_data[s_data_ind]);
      |                                        ^~~~~~~~~~~~~~

/home/ywt01/Documents/codes/nccu_cs_hw/DataStructure_HW/HW3/hw3.cpp:815:62: error: ‘root’ was not declared in this scope
  815 |                 TreapNode<int> *res =  data_structure.search(root, search_data[s_data_ind]);
      |                                                              ^~~~

/home/ywt01/Documents/codes/nccu_cs_hw/DataStructure_HW/HW3/hw3.cpp:820:28: error: ‘data_structure’ was not declared in this scope
  820 |                 bool res = data_structure.search(search_data[s_data_ind]);
      |                            ^~~~~~~~~~~~~~

Build finished with error(s).

CodePudding user response:

The variables data_structure and root are alive and visible only in block scopes pf if statements where they are declared as for example in this if statement

    else if (type_record.compare("treap"))
    {
        cout << "start test treap insert & search\n";
        TreapNode<int> data_structure;
        TreapNode<int> *root = nullptr;
    }

If you will even write

    else if (type_record.compare("treap"))
        TreapNode<int> data_structure, *root = nullptr;

nevertheless the sub-statement of the if statement forms its own block scope. That is the above statement is equivalent to

    else if (type_record.compare("treap"))
    {
        TreapNode<int> data_structure, *root = nullptr;
    }  

You need to declare the variables outside if statements if they are required in other parts of the function.

  • Related