gpt4 book ai didi

c++ - 没有从 'Foo' 到 'Foo *const' C++ 的可行转换

转载 作者:太空狗 更新时间:2023-10-29 23:08:46 26 4
gpt4 key购买 nike

我有一个用 C++ 实现图(模板类)的类(我没有做这个),基本上我想做的是表示 Places(它们是我的顶点)和我的边(连接它们)是距离(以英里为单位)。

我声明了一个简单的 Graph<Places *, double> places;现在我正在创建一个方法来检查某个地方是否已经在图中以避免重复。我的图表有一个方法 Vertice<TV,TR>* findvert_content(const TV& v) const;返回它找到的顶点的指针(所以如果返回的指针有效,我假设它存在于图中)。现在我想在我的测试类中这样做:

bool Test::verifiesName(Place *place) { 
Vertice<Place *, double> find_place = places.findvert_content(*place);
...
}

问题是它给了我一个语义问题:“没有从‘Place’到‘Place *const’的可行转换”,我不知道从这里该做什么。

下面是我的findvert方法的代码(供引用):

template<class TV,class TR>
Vertice<TV,TR>* Graph<TV,TR>::findvert_content(const TV& v) const
{
Vertice<TV,TR>* ap=graf;

while (ap != NULL)
{
if (ap->vcontent == v)
return ap ;
else
ap=ap->apvertice;
}
return ap;
}

附言graf 只是我的 Graph 类中的一个属性:Vertice<TV,TR>* graf; .

最佳答案

*placeplace 指向的地址的值。您确定要在调用方法时传递值而不是指针本身吗?喜欢:

Vertice<Place *, double> find_place = places.findvert_content(place);

此外,如果您不在方法内部修改 v,为什么还需要 & ?然后声明

template<class TV,class TR>
Vertice<TV,TR>* Graph<TV,TR>::findvert_content(const TV v) const

并且它应该与上面的调用兼容。

关于c++ - 没有从 'Foo' 到 'Foo *const' C++ 的可行转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8249417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com