首页 > 解决方案 > C++ 成员变量被非成员函数覆盖

问题描述

我正在开发一个使用linkedList我编写的类的项目。列表中的每个节点都包含一个字符串,当对象外部有任何函数调用时,该字符串似乎被覆盖。该变量是私有的,并且只能在构造函数中设置。这是重现错误的代码的精简版本:

链接列表.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函数将在列表的第 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;
}

输出:

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

一些 gdb 输出:

(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

标签: c++oop

解决方案


这个问题很愚蠢,我忘记使用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;
  }

推荐阅读