将数据从结构加载到该结构的向量中时出现问题
Issues loading data from a struct into a vector of that struct
该结构仅在我的类的一个函数中。我一直在调试并尝试我能想到的一切。这是针对图形的,该函数用于Dijkstra公式。我遇到的主要问题是我的数据永远不会进入我的向量(向量打开)。
不确定是否需要所有代码,因为所有问题都发生在此函数中。当前未使用的代码(尝试先将数据放入向量)已被注释掉。
void Graph::Dijkstra(string start, string end, vector<string> &path)
{
struct node
{
string name;
vector<string> connection;
int costSoFar;
node() {name = " "; costSoFar = 0;}
node(node& other)
{
name = other.name;
connection = other.connection;
costSoFar = other.costSoFar;
}
};
vector<string> adjacent;
node startNode;
node current;
node endNode;
vector<node> open;
node closed[MAX_VERTICES];
int small, temp;
bool found = false;
bool inClosed = false;
bool inOpen = false;
string tempVertex;
// starting node is added open list
// startNode = node();
startNode.name = start;
startNode.costSoFar = 0;
//adjacent.push_back(startNode.name);
open.push_back(startNode);
temp = 0; // used a place holder for debugging
//open[0].name = startNode.name;
//open[0].costSoFar = startNode.costSoFar;
}
任何帮助将不胜感激。我看过类似的帖子并尝试了他们的建议,不确定为什么即使我尝试直接应用它,我的向量也不会接受输入(请参阅上面的注释代码)。
我相信 std 容器需要表单的复制构造函数:
node(const node& other);
这是默认复制构造函数的形式(如果未提供)。如果确实提供了复制构造函数,则不会为您提供此默认构造函数,因此您必须定义该表单之一。
好的,
我找到了自己的解决方案。如果结构是在函数内部创建的,则它无法以所需的方式访问变量。我所做的只是将节点结构移动到函数的外部。
struct node
{
string name;
vector<string> connection;
int costSoFar;
node() {name = " "; costSoFar = 0;}
node(const node& other)
{
name = other.name;
connection = other.connection;
costSoFar = other.costSoFar;
}
};
void Graph::Dijkstra(string start, string end, vector<string> &path)
{ // the other code needed }
相关文章:
- 将结构向量排序为子组
- NLOHMANN 的 JSON 库将数组转换为结构向量
- 如何在 c++ 中创建结构向量的映射
- 带unique_ptr的结构向量
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 将信息输入到下面显示的结构向量中的正确语法/格式是什么
- C++对结构向量进行排序
- 在C++中对结构向量进行排序时出现问题
- 将结构向量保存到文件中,并从C++文件中读取结构向量
- 搜索并替换结构向量中的值
- 在结构向量中查找最接近的值
- 清除结构向量时会发生什么?
- 如何从 node-ffi 调用 c++ 中以结构向量作为参数的方法?
- 对结构向量进行两次排序
- 将一种数据类型的向量复制到同一数据类型的结构向量中的有效方法是什么
- 如何二进制搜索结构向量并在适当的索引处插入
- 结构向量中自定义结构函数的内存使用
- C++11-在结构向量中修改结构的成员
- 我正在尝试找到一个特定的结构成员,该成员是使用 lambda 函数的结构向量
- 在结构向量中搜索一个数据成员,然后打印匹配的所有数据成员