【发布时间】:2021-08-22 03:52:19
【问题描述】:
我有一个结构
struct Node
{
string Name;
vector<float> probs;
vector<Node*> connections;
};
我有一个名为 Input 的字符串 vector 每行如下
0 0 1 0 0
0 0 1 0 0
0 0 0 1 1
0 0 0 0 0
0 0 0 0 0
如果我找到 1,我想将 .push_back() 转换为 connections
我用了以下
int N=5;
Node Tables[N];
for (int i = 0; i < N*2-1; ++i)
{
for (int j = 0; j < N*2-1; ++j)
{
if (Input[i][j] == '1')
{
Tables[i].connections.push_back(Tables[j]);
}
}
}
编译时出现此错误
main.cpp:128:31: error: no matching member function for call to 'push_back'
Tables[i].connections.push_back(Tables[j]);
~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
【问题讨论】:
-
connections是 pointers 到Node的向量,而Tables[j]是Nodeobject(而不是指针)。 -
是的,我知道我需要
Tables[i].connections.push_back(&Tables[j]); -
旁白:
i < N*2-1是一个非常可疑的循环边界 -
是的@Caleth 导致了一个问题,我通过去掉向量中的所有空格来解决它,所以它是一个 5x5
标签: c++ variables vector struct