二进制'[':找不到运算符,它接受类型'const std :: map '的左手操作数

时间:2020-12-22 16:33:24

I don't know where the error is coming from. It seems like I'm passing valid data into the [ ] operator.

我不知道错误来自哪里。好像我将有效数据传递给[]运算符。

template <class VertexType>
typename map< Vertex<VertexType>, int >::iterator Graph<VertexType>::findEdge( const VertexType& v, const VertexType& w ) const
{
    map<Vertex<VertexType>, int>::const_iterator iter = vertices[v].second.adjList.find(w);

    return iter;
}  // end findEdge  

The error:

error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const std::map<_Kty,_Ty>' (or there is no acceptable conversion)
1>          with
1>          [
1>              _Kty=unsigned int,
1>              _Ty=Vertex<unsigned int>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\map(164): could be 'Vertex<VertexType> &std::map<_Kty,_Ty>::operator [](unsigned int &&)'
1>          with
1>          [
1>              VertexType=unsigned int,
1>              _Kty=unsigned int,
1>              _Ty=Vertex<unsigned int>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\map(209): or       'Vertex<VertexType> &std::map<_Kty,_Ty>::operator [](const unsigned int &)'
1>          with
1>          [
1>              VertexType=unsigned int,
1>              _Kty=unsigned int,
1>              _Ty=Vertex<unsigned int>
1>          ]
1>          while trying to match the argument list '(const std::map<_Kty,_Ty>, const unsigned int)'
1>          with
1>          [
1>              _Kty=unsigned int,
1>              _Ty=Vertex<unsigned int>
1>          ]
1>          c:\users\user\desktop\cs m20a\topic g project 2\topic g project 2\graph.h(146) : while compiling class template member function 'std::_Tree_iterator<_Mytree> Graph<VertexType>::findEdge(const VertexType &,const VertexType &) const'
1>          with
1>          [
1>              _Mytree=std::_Tree_val<std::_Tmap_traits<Vertex<unsigned int>,int,std::less<Vertex<unsigned int>>,std::allocator<std::pair<const Vertex<unsigned int>,int>>,false>>,
1>              VertexType=unsigned int
1>          ]
1>          c:\users\user\desktop\cs m20a\topic g project 2\topic g project 2\graph.h(52) : while compiling class template member function 'Graph<VertexType>::Graph(unsigned int)'
1>          with
1>          [
1>              VertexType=unsigned int
1>          ]
1>          c:\users\user\desktop\cs m20a\topic g project 2\topic g project 2\topicg.cpp(17) : see reference to class template instantiation 'Graph<VertexType>' being compiled
1>          with
1>          [
1>              VertexType=unsigned int
1>          ]

And the Graph class:

而Graph类:

template <class VertexType>
class Graph
{
private:
     // list of all vertices in the graph. assumes non-duplicate data.
    map< VertexType, Vertex<VertexType> > vertices;

    const unsigned MAX_VERTICES;  // Maximum number of vertices the graph can hold.
    unsigned numVertices;         /** Current number of vertices in the graph. */
    unsigned numEdges;            /** Number of edges in the graph. */

    typename map< Vertex<VertexType>, int >::iterator findEdge( const VertexType& v, const VertexType& w ) const;  

public:
   Graph( unsigned max );

   unsigned getNumVertices() const;
   unsigned getMaxNumVertices() const;
   unsigned getNumEdges() const;
   int getWeight( const VertexType& v, const VertexType& w ) const;

   Graph<VertexType>& addVertex( const VertexType& newValue );
   Graph<VertexType>& addEdge( const VertexType& v, const VertexType& w, int weight );
   void removeEdge( const VertexType& v, const VertexType& w );
   void BFS( const VertexType& v ) const;
   void display() const;
}; // end Graph

2 个解决方案

#1


35  

operator[] can only be called on a non-const map.

operator []只能在非const映射上调用。

If the key doesn't exist, it will be inserted with a default constructed value.

如果密钥不存在,则将使用默认构造值插入密钥。

To get a const iterator, use map::find.

要获取const迭代器,请使用map :: find。

#2


2  

While not the case above, the same problem will occur if you use the [] operator in a const function.

虽然不是上述情况,但如果在const函数中使用[]运算符,也会出现同样的问题。

#1


35  

operator[] can only be called on a non-const map.

operator []只能在非const映射上调用。

If the key doesn't exist, it will be inserted with a default constructed value.

如果密钥不存在,则将使用默认构造值插入密钥。

To get a const iterator, use map::find.

要获取const迭代器,请使用map :: find。

#2


2  

While not the case above, the same problem will occur if you use the [] operator in a const function.

虽然不是上述情况,但如果在const函数中使用[]运算符,也会出现同样的问题。