使用 std::stack::p ush 函数时无法从结构中读取字符串C++
Can't read string from struct when using C++ std::stack::push function
我为我的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;
}
我修复了三件事。
- .c_str ()
- char tempCharArray [9];-> char tempCharArray[10];
- testNode->path = "0dddddddd";
相关文章:
- C++结构到字符串
- 从字符串中获取结构的大小
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 如何解析表示树状数据结构的字符串
- 如何将字符串与结构(也包括字符串)进行比较?
- 如果结构中的字符串比使用的 p/调用签名长或短,会发生什么情况?
- 在向量 C++14(无限制联合)的结构内的联合中创建和存储字符串
- C++ - 使用用户输入的字符串数据检查结构字符串数据(无限执行 while 循环)
- 将字符串存储在 constexpr 结构中
- 为什么我的结构在包含字符串时崩溃?
- 如何将字符串字符与结构数组进行比较?
- 无法使用 strcpy 将字符串复制到静态结构成员
- c++ 将函数参数(带有模板变量的结构)转换为函数体内的字符串
- 在 c++ 中使用字符串变量选择静态类结构,而不带对象
- 如何输出属于向量数组的结构字符串
- 为什么我不能像正常一样初始化 typedef 结构字符串或字符?
- 如何引用结构字符串成员
- c++运算符+=重载结构字符串
- 如何将平面列表合并转换为层次结构字符串
- 如何在c++中比较两个结构字符串