使用C ++ std :: stack :: push函数时,无法从struct读取字符串

时间:2014-03-23 06:27:04

标签: c++ pointers struct stack

我为我的8个益智项目编写了这个测试代码。问题是line23字符串currentPath无法从Node读取任何内容。调试消息是“无法评估函数 - 可以内联”。结果,功能无法通过开关循环。 我之前没有类似的项目,它们都运作良好。任何人都知道这是什么问题?

在屏幕的第3行,它假设打印字符串变量“currentPath”,但现在它什么都没有。结果会弹出错误语句。     #包括     #包括     #包括     #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!\r\nExit." << 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;
}

1 个答案:

答案 0 :(得分: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!\r\nExit." << 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]; - &GT; char tempCharArray [10];
  3. testNode-&gt; path =&#34; 0dddddddd&#34;;
  4. 我可能会误解你的问题。但那段代码正在运作。