static int index = 0;
class neighbor
{
public:
int dest ;
int weight;
neighbor( int d= 0, int w= 0);
};
template<typename T>
class vertexInfo
{
public:
enum vertexColor { white , grey , black } ;
typename map< T, int>:: iterator vtxMapLoc;
set<neighbor> edges;
vertexInfo();
// default constructor
vertexInfo( const map<T, int> :: iterator& iter)
{
// constructor with iterator pointing to a vertex in map
}
int inDegree;
};
template <typename T>
class graph
{
private:
typename map< T, int > :: iterator iter;
map <T,int> vtxMap;
vector<vertexInfo<T> > vInfo;
int numVertices;
int numedges;
stack<int> availStack;
int getvInfoIndex(graph<T>& , const T& v);
public:
void addEdge( graph<T>& , const T& , const T& , int );
set<T> get_Neighbor( graph<T>& , const T& v) ;
void show(graph<T>& );
};
template <typename T>
void graph<T> :: addEdge ( graph<T> & g, const T& v1 , const T& v2, int w)
{
pair <map <char, int> :: iterator, bool> ret ;
ret = g.vtxMap.insert(pair <char, int >( v1, index));
if( ret.second)
{
index++;
g.vInfo.push_back(vertexInfo<T>(index));// Error -> 1
}
ret = g.vtxMap.insert(pair <char, int >(v2 , index));
if( ret.second)
{
index++;
g.vInfo.push_back(index)); // Error -> 2
}
}
我想将"index"
值推送到vInfo
向量。但是得到错误。
Error 1 - > `No matching function for call to 'vertexInfo<char>:: vertexInfo(Int&)`
Error 2 -> `No matching function for call to std :: vector <vertexInfo<char> , std :: allocator <vertexInfo<char> > > push_back(int&)`
我尝试通过两种方式推送元素但仍然出错 如何删除此错误?
UPDATE :
通过查看回复,我更新了代码的正文。添加了两个构造函数
vertexInfo() //Default constructor
vertexInfo( const map<T, int> :: iterator& iter)
{
// constructor with iterator pointing to a vertex in map i.e. it is initializes the vtxMapLoc data member with the location of the vertex in the map.
}
但不了解如何编写构造函数体。
答案 0 :(得分:0)
第一个错误
No matching function for call to 'vertexInfo<char>:: vertexInfo(Int&)
意味着没有类型为vertexInfo<char>
的构造函数,它将Int
(代码示例中的变量类型index
)作为参数。只需创建这样的构造函数,一切都会有效。
至于第二个错误,我不清楚你想要达到什么样的行为。您正尝试将push_back
int
值加入vector
vertexInfo<char>
。如果您希望它创建相应的vertexInfo<char>
,则应该使用emplace_back
或vertexInfo<T>
构造函数,如第一个示例所示。无论如何,您将需要定义构造函数。