繁体   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