C++ 声明和范围问题

Posted

技术标签:

【中文标题】C++ 声明和范围问题【英文标题】:C++ declaration and scope issues 【发布时间】:2022-01-21 12:07:48 【问题描述】:

我遇到了错误:error: ‘data_structure’ was not declared in this scope, 编译以下代码时。

我假设错误发生在:

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]);

但我不知道我应该纠正什么。

完整代码:https://gist.github.com/theabc50111/05651b8c125feaaff5f80f15deb535f4

部分代码:

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;

错误:

/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).

【问题讨论】:

变量仅在创建它们的大括号中可见(或多或少),并且仅在控件位于这些大括号内时才存在。 您可以将测试放在模板中,例如godbolt.org/z/KorT531vc 【参考方案1】:

变量 data_structureroot 仅在块作用域 pf if 语句中有效且可见,其中它们被声明为例如在此 if 语句中声明

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

如果你会写

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

尽管如此,if 语句的子语句形成了它自己的块作用域。也就是上面的语句等价于

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

如果函数的其他部分需要变量,则需要在 if 语句之外声明变量。

【讨论】:

谢谢,现在我知道python和C++的区别了

以上是关于C++ 声明和范围问题的主要内容,如果未能解决你的问题,请参考以下文章

在 C++ 中在哪里声明/定义类范围常量?

C++ 错误:未在此范围内声明类/对象

C++ 错误:“QueryFullProcessImageNameA”未在此范围内声明

C++ Win API 函数'未在此范围内声明'

字符串未在此范围内声明 C++

什么是 C++ 中的“变量‘i’未在范围内声明”?