我有一个名为 street_map 的类,其中包含一个带有 int 键和 type 值的地图vector<edge>
。在其中一种方法中,我试图初始化一个指向该vector<edge>
值的指针以获取其内容。
class street_map {
public:
explicit street_map (const std::string &filename);
bool geocode(const std::string &address, int &u, int &v, float &pos) const;
bool route3(int source, int target, float &distance) const {
auto it = adjacencyList.find(source);
vector<edge>* v = &(it->second);
return true;
}
private:
unordered_map<side , vector<segment>> map;
unordered_map<int, vector<edge>> adjacencyList;
};
该行vector<edge>* v = &(it->second);
给出了错误:
Cannot initialize a variable of type 'vector<edge> *' with an rvalue of type 'const std::__1::vector<edge, std::__1::allocator<edge> > *'
这是边缘类:
class edge {
int node;
string street;
float length;
int startingNode;
public:
edge(int startingNode, int node, string street, float length) {
startingNode = startingNode;
node = node;
street = street;
length = length;
}
};
我想知道这是否是因为 const 关键字以及如果是因为 const 关键字如何解决这个问题(我应该保留 const 关键字,但我想如果没有其他解决方案我可以摆脱它)。