簡體   English   中英

C++ 成員變量被非成員函數覆蓋

[英]C++ Member variables being overwritten by non-member functions

我正在開發一個使用我編寫的linkedList class 的項目。 列表中的每個節點都包含一個字符串,當 object 之外有任何 function 調用時,該字符串似乎被覆蓋。 該變量是私有的,並且只能在構造函數中設置。 這是重現錯誤的代碼的精簡版本:

鏈接列表.h:

#include <cstring>
#include <iostream>

class Node{
private:
  std::string value;
  Node* prev_node;
public:
  Node* next_node = nullptr;
  Node(std::string value_in, Node* prev){
    this->value = value_in;
    this->prev_node = prev;
    this->next_node = nullptr;
  }

  void print_value(){
    std::cout << "value = " << this->value << '\n';
  }
  void print_prev(){
    std::cout << "prev = " << this->prev_node << '\n';
  }
  Node* get_prev(){
    return this->prev_node;
  }
};

class linkedList{
public:
  Node start_node = Node((std::string) "", (Node*) nullptr);
  Node* current_node = &start_node;

  void prepend_node(std::string value){
    Node new_node = Node(value, &start_node);
    new_node.print_value();
    new_node.next_node = start_node.next_node;
    start_node.next_node = &new_node;
  }

  void return_to_start(){
    current_node = &start_node;
  }

  void scroll_up(){
    if(current_node->next_node){
      current_node = current_node->next_node;
    }
  }

  void scroll_down(){
    if(current_node->get_prev()){
      current_node = current_node->get_prev();
    }
  }
};

(這里需要注意的一點是prepend_node function 將在列表的 position 2 處插入一個節點。這是故意的)

主.cpp:

#include <iostream>
#include "linkedList.h"

int main(){
  linkedList foo;

  foo.prepend_node("value1");
  foo.prepend_node("value2");

  foo.current_node->next_node->print_value();

  foo.scroll_up();
  foo.current_node->print_value();

  foo.scroll_up();
  foo.current_node->print_value();
  return 0;
}

output:

value = value1
value = value2
value = 
value = 
Segmentation fault

一些 gdb output:

(gdb) b linkedList.h:20
Breakpoint 1 at 0x101e: file linkedList.h, line 20.
(gdb) run
Starting program: /path/to/main

Breakpoint 1, Node::print_value (this=0x7fffffffdd50) at linkedList.h:20
20      std::cout << "value = " << this->value << '\n';
(gdb) p this->value
$1 = "value1"
(gdb) c
Continuing.
value = value1

Breakpoint 1, Node::print_value (this=0x7fffffffdd50) at linkedList.h:20
20      std::cout << "value = " << this->value << '\n';
(gdb) c
Continuing.
value = value2

Breakpoint 1, Node::print_value (this=0x7fffffffdd50) at linkedList.h:20
20      std::cout << "value = " << this->value << '\n';
(gdb) c
Continuing.
value = 

Breakpoint 1, Node::print_value (this=0x7fffffffdd50) at linkedList.h:20
20      std::cout << "value = " << this->value << '\n';
(gdb) this->value
Undefined command: "this->value".  Try "help".
(gdb) c
Continuing.
value = 

Breakpoint 1, Node::print_value (this=0xa00000000) at linkedList.h:20
20      std::cout << "value = " << this->value << '\n';
(gdb) c
Continuing.

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b735b0 in std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()
   from /usr/lib/x86_64-linux-gnu/libstdc++.so.6

這個問題很愚蠢,我忘了使用new 下面是一個可以工作的prepend_node的固定版本:

void prepend_node(std::string value){
    Node* new_node = new Node(value, &start_node);
    new_node->print_value();
    new_node->next_node = start_node.next_node;
    start_node.next_node = new_node;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM