简体   繁体   中英

No viable conversion from 'Foo' to 'Foo *const' C++

I have a class that does an implementation of a graph (template class) in C++ (I didn't make this), and basically what I want to do is have a representation of Places (they are my vertices) and my edges (that link them) are distances (in miles).

I declared a simple Graph<Places *, double> places; and now I'm creating a method that checks check if a certain Place is already in the graph in order to avoid duplicates. My graph has a method Vertice<TV,TR>* findvert_content(const TV& v) const; that returns a pointer of an vertice it finds (so if that returning pointer is valid, I'm assuming it exists in the graph). Now I want to do this in my Test class:

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

The problem is it gives me an semantic issue: "No viable conversion from 'Place' to 'Place *const'" and I don't know what to do from here.

Here is the code of my findvert method (for reference):

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;
}

PS graf is just an attribute in my Graph class: Vertice<TV,TR>* graf; .

*place is the value at the address where place points to. Are you sure you want to pass the value and not the pointer itself when you call the method? Like:

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

Also, why do you need the & if you are not modifying v inside the method? Then declare

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

and it should be compatible with the above call.

TV在你的类型places成员需要一个指向一个Place

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM