【发布时间】:2014-01-10 13:41:51
【问题描述】:
我正在尝试访问我的嵌套类,以便可以在此函数中返回对象:
Graph::Edge Graph::get_adj(int i)
{
Graph::Edge v;
int count = 0;
for(list<Edge>::iterator iterator = adjList[i].begin(); count <= i ;++iterator)
{
v.m_vertex = iterator->m_vertex;
v.m_weight = iterator->m_weight;
}
return v;
}
不用担心 for 循环(理论上它应该可以工作)我的主要问题是声明对象 Graph::Edge v; 它不起作用!这是我得到的错误:
$ make -f makefile.txt
g++ -Wall -W -pedantic -g -c Graph.cpp
Graph.cpp: In member function `Graph::Edge Graph::get_adj(int)':
Graph.cpp:124: error: no matching function for call to `Graph::Edge::Edge()'
Graph.cpp:43: note: candidates are: Graph::Edge::Edge(const Graph::Edge&)
Graph.h:27: note: Graph::Edge::Edge(std::string, int)
makefile.txt:9: recipe for target `Graph.o' failed
make: *** [Graph.o] Error 1
我想访问
Graph.h:27: note: Graph::Edge::Edge(std::string, int)
这是我的类 Graph 的声明方式:(为了简单起见,我取出了函数和一些东西,使其更易于阅读) *
class Graph
{
private:
vector< list<Edge> > adjList;
public:
Graph();
~Graph();
class Edge
{
public:
Edge(string vertex, int weight)
{
m_vertex = vertex;
m_weight = weight;
}
~Edge(){}
string m_vertex;
int m_weight;
};
vector < list < Edge > > get_adjList(){return adjList;}
//Other functions....
};
基本上,我只需要知道在此函数中声明 Edge 对象的正确方法。我真的很困惑,不知道除了 Graph::Edge v 还能做什么;
【问题讨论】:
-
我不明白
get_adj(i)的意思,它应该返回顶点i的整个邻接表还是一些邻接表的i-th邻接项未指定 顶点?我假设您的adjList实际上是每个顶点的邻接列表,这意味着adjList[i]包含顶点i的邻接。