我制作了一个相互连接的节点图。
我创建了一个名为nvec的节点向量和一个名为nmatrix的节点向量矩阵:
typedef vector <node> nvec;
typedef vector <nvec> nmatrix;
我有一个图形结构,我将图形定义为&#39; g&#39;在我的计划中:
struct graph
{
nmatrix edges; //list of attached vertices for each node
int numVertices;
};
我还有节点本身的结构,每个节点都包含一个int和一个字符串
struct node
{
int num;
string word;
};
这是我的主要功能,后跟我的函数在图形中插入/连接节点
int main()
{
svec storage;
ifstream inputFile;
string filename;
graph g;
initGraph(&g);
cout << "Please enter the name of a text file. e.g. example.txt" << endl;
getline (cin, filename); //Get the filename
while (true) //Determine if the filename is valid and the file is openable
{
inputFile.open(filename.c_str(), ifstream::in);
if (!inputFile.is_open()) //If not openable, ask for another name
{
cout << "Could not open " << filename << endl;
cout << "Please enter another filename." << endl;
getline (cin, filename);
}
else //Otherwise break
{
break;
}
}
//Fill graph
while (inputFile.good()) //Check if there are still lines left to check
{
//Loop through each line of the text file
string line;
getline (inputFile, line);
storage.push_back(line);
}
node * Node = NULL;
node * Edge = NULL;
for (int i = 1; i <= storage.size(); i++)
{
for (int j = 1; j <= storage.size(); j++)
{
if (i == j)
{
continue;
}
else
{
Node = createNewNode(i, storage[i]);
Edge = createNewNode(j, storage[i]);
insertEdge(&g, Edge, Node);
}
}
}
string beginWord = "blood";
string endWord = "tears";
cout << "The word distance between " << beginWord << " and " << endWord << " is " << endl;
//shortestPath(&g, beginWord, endWord);
return 0;
}
//Function to insert/connect nodes
void insertEdge(graph * g, node * Node, node * Edge)
{
int nodeNum = Node->num;
int edgeNum = Edge->num;
int numVertices = max(nodeNum, edgeNum);
numVertices = max(1, numVertices);
if (numVertices > g->numVertices)
{
for (int i = g->numVertices; i <= numVertices; i++)
{
nvec nodes;
if (g->edges.size() < i)
{
g->edges.push_back(nodes);
}
}
g->numVertices = numVertices;
}
g->edges[nodeNum - 1].push_back(Edge);
}
我遇到的问题是,当我在insertEdge函数中运行程序时,以下行:
g->edges[nodeNum - 1].push_back(Edge);
给出了这个错误:
error: no matching function for call to 'std::vector<node>::push_back(node*&)'
我很困惑,因为我没有将我的节点定义为节点*&amp;&#39;那为什么要打电话呢?
很抱歉,如果这是初学者问题,这是我第一次使用图表时,我不完全了解如何在其中实现节点。