0

我目前对正在制作的双向链表类的 get_set() 函数调用存在问题。该函数返回列表中从position_fromposition_to的所有元素的向量。对于我发送的测试

dll_UT->get_set(dll_UT->size()/2+1, dll_UT->size()-1)

但是, dll_UT->size() 的值在调用中以某种方式加倍。在我的 COUT 语句之前直接调用时,它显示正确的值 100,但在函数内部时,它变为 200。我在下面留下了控制台日志以及 test.cpp 和 doubly_linked_list.cpp 的屏幕截图。

非常感谢您对此提供的任何帮助,因为我以前从未见过这种行为,并且想了解为什么会发生这种情况。

Doubly_linked_list get_set(unsigned position_from, unsigned_position to){

 std::cout << "\n\t__NEW TEST__\nSize of size/2+1 = " << this->size()/2+1 << "\nSize of size()-1 = " << this->size()-1 << std::endl;
        std::cout << "position_from = " << position_from << "\nposition_to = " << position_to << std::endl;
         //value of position_from == 200, should be == this->size()/2+1 which = 100
        

        std::vector<int> temp;
        temp.reserve(position_to - position_from);

        while(position_from < position_to){
            temp.push_back( this->get_data(position_from) );
            position_from++;
        }
        
        temp.push_back(this->get_data(position_to));
      
        std::cout << "Size of position_from = " << position_from << "\nSize of position_to = " << position_to << "\n\t__END TEST__" << std::endl;

        return temp;```
}

Test.cpp {
```std::cout << "\nTEST.cpp::: dll_UT-size() = " << dll_UT->size() << 
    "\nTEST.cpp::: dll_UT-size()/2+1 = " << dll_UT->size()/2+1 << std::endl;
      //assuring dll_UT->size()/2+1 = 100     

    std::cout << "\nTEST.cpp::: expected_set.size() = " << expected_set.size() << 
    "\nTEST.cpp::: expected_set.begin()+expected_set.size()/2+1 = " << expected_set.size()/2+1 << std::endl;

    ASSERT_EQ(std::vector<int> (expected_set.begin()+expected_set.size()/2+1,expected_set.end()), 
                                dll_UT->get_set( dll_UT->size()/2+1 , dll_UT->size()-1));//second half of data
                                //               ^first operand doubled, ^second operand fine
//actually value sent is 200. dll_UT->size() is somehow doubling and equaling 400 but only for first operand of get_set(unsigned, unsigned)

更新的 Caputre

4

0 回答 0