使用 std::stack::p ush 函数时无法从结构中读取字符串C++

Can't read string from struct when using C++ std::stack::push function

本文关键字:结构 字符串 C++ 读取 stack std ush 使用 函数      更新时间:2023-10-16

我为我的8个谜题项目编写了这个测试代码。问题是line23字符串currentPath无法从节点读取任何内容。调试的消息是"无法计算函数——可能是内联的"。结果,该函数不能通过开关循环。我以前没有类似的项目,它们都很好用。有人知道是什么问题吗?

在屏幕的第3行,它应该打印字符串变量"currentPath",但现在它什么也没有。结果弹出错误语句。# include# include# include#include//string::size_type#include//std::swap

using namespace std;
struct Node{
    string state;
    string path;
    int depth;
};
Node dequeue;
stack<Node> enqueue;
stack<Node> visitedList;
void AddNextPath(Node *&listpointer){
    Node *temp, *newNode;
    temp = listpointer;
    newNode = new Node;
    string currentPath = listpointer->path;
    cout << currentPath << endl;
    if(currentPath.length() == 9){
        string::size_type location = currentPath.find("0"); // Finde char index of '0' in the string
        char tempCharArray[9];
        strcpy(tempCharArray, currentPath.c_str()); // Convert the string to char array
        string newState;
        // The required order for traverse each state is U,R,D,L.
        // When add new node to stack, this order has to done in reverse order: L,D,R,U
        switch (location){
        // Each case represents a location of 3x3 graphic map
        case 0:
            // Move down
            swap(tempCharArray[0],tempCharArray[3]);
            newState = tempCharArray;
            newNode->state = newState;
            newNode->path = temp->path.append("D");
            newNode->depth = temp->depth + 1;
            enqueue.push(*newNode);
            // Move right
            swap(tempCharArray[0],tempCharArray[1]);
            newState = tempCharArray;
            newNode->state = newState;
            newNode->path = temp->path.append("R");
            newNode->depth = temp->depth + 1;
            enqueue.push(*newNode);
            break;
//        case 1:
        }
    }
    else{
        cout << "The length of the current state is " << currentPath.length() << endl;
        cout << "Warning: state length error!rnExit." << endl;
        exit(0);
    }
}
int main()
{
    Node *testNode = new Node;
    testNode->depth = 0;
    testNode->state = "087654321";
    testNode->path = "";
    cout << "The current state is " << testNode->state << endl;
    cout << "The path is " << testNode->path << endl;
    enqueue.push(*testNode);
    AddNextPath(testNode);
    cout << "The size of enqueue is" << enqueue.size() << endl;
    Node topNode;
    topNode = enqueue.top();
    cout << "The top state is " << topNode.state << endl;
    cout << "The path is " << topNode.path << endl;
    cout << "The depth is " << topNode.depth << endl;
    return 0;
}
#include <stack>
#include <iostream>
using namespace std;
struct Node{
    string state;
    string path;
    int depth;
};
Node dequeue;
stack<Node> enqueue;
stack<Node> visitedList;
void AddNextPath(Node *&listpointer){
    Node *temp, *newNode;
    temp = listpointer;
    newNode = new Node;
    string currentPath = listpointer->path;
    cout << currentPath.c_str() << endl;
    if(currentPath.length() == 9){
        string::size_type location = currentPath.find("0"); // Finde char index of '0' in the string
        char tempCharArray[10];
        strcpy(tempCharArray, currentPath.c_str()); // Convert the string to char array
        string newState;
        // The required order for traverse each state is U,R,D,L.
        // When add new node to stack, this order has to done in reverse order: L,D,R,U
        switch (location){
        // Each case represents a location of 3x3 graphic map
        case 0:
            // Move down
            swap(tempCharArray[0],tempCharArray[3]);
            newState = tempCharArray;
            newNode->state = newState;
            newNode->path = temp->path.append("D");
            newNode->depth = temp->depth + 1;
            enqueue.push(*newNode);
            // Move right
            swap(tempCharArray[0],tempCharArray[1]);
            newState = tempCharArray;
            newNode->state = newState;
            newNode->path = temp->path.append("R");
            newNode->depth = temp->depth + 1;
            enqueue.push(*newNode);
            break;
//        case 1:
        }
    }
    else{
        cout << "The length of the current state is " << currentPath.length() << endl;
        cout << "Warning: state length error!rnExit." << endl;
        exit(0);
    }
}
int main()
{
    Node *testNode = new Node;
    testNode->depth = 0;
    testNode->state = "087654321";
    testNode->path = "0dddddddd";
    cout << "The current state is " << testNode->state.c_str() << endl;
    cout << "The path is " << (testNode->path).c_str() << endl;
    enqueue.push(*testNode);
    AddNextPath(testNode);
    cout << "The size of enqueue is" << enqueue.size() << endl;
    Node topNode;
    topNode = enqueue.top();
    cout << "The top state is " << topNode.state.c_str() << endl;
    cout << "The path is " << topNode.path.c_str() << endl;
    cout << "The depth is " << topNode.depth << endl;
    return 0;
}

我修复了三件事。

  1. .c_str ()
  2. char tempCharArray [9];-> char tempCharArray[10];
  3. testNode->path = "0dddddddd";
我可能误解了你的问题。但是代码是有效的