C++ 无法从常量T转换为常量T&;

C++ 无法从常量T转换为常量T&;,c++,C++,我有一个方法void-Graph::remove_-vertex(const T&T) 调用graph.get(t) 图形属于列表类型 它有一个T&get(const T&T)我无法调用的方法 List是我自己实现的std::List 我收到的错误消息是: Error C2664 'T &List<T>::get(const T &) const': cannot convert argument 1 from 'const T' to 'const T &a

我有一个方法
void-Graph::remove_-vertex(const T&T)
调用
graph.get(t)

图形
属于
列表类型

它有一个
T&get(const T&T)我无法调用的方法

List
是我自己实现的
std::List

我收到的错误消息是:

Error   C2664   'T &List<T>::get(const T &) const': cannot convert argument 1 from 'const T' to 'const T &' O2  c:\users\sorn\source\repos\o2\o2\graph.h    80  
错误C2664'T&List::get(const T&)const':无法将参数1从'const T'转换为'const T&'O2 c:\users\sorn\source\repos\O2\O2\graph.h 80

List.h

#pragma once

template<typename T>
class List
{
public:
    struct Iterator;

    List();
    ~List();

    void push_back(const T&);
    void push_front(const T&);
    void pop_back();
    void pop_front();
    void clear();
    Iterator erase(Iterator it);

    T& front() const;
    T& back() const;
    T& get(const T&) const;
    int size() const;
    bool empty() const;
    Iterator begin();
    Iterator end();
protected:
    struct Link;
    struct Node;

    void unlink(Link*);

    Link* sentinel_;
    int size_;
};

template<typename T>
struct List<T>::Link
{
    Link() : next(this), prev(this) { /**/ }
    Link* next;
    Link* prev;
};

template<typename T>
struct List<T>::Iterator
{
    Iterator() = default;
    Iterator(Node* n) : node(n) {}
    T& operator*() { return node->value; }
    bool operator==(const Iterator& rhs) { return (node->value == rhs.node->value); }
    bool operator!=(const Iterator& rhs) { return (node->value != rhs.node->value); }
    Iterator& operator++()
    { 
        node = static_cast<Node*>(node->next);
        return *this;
    }
private:
    Node* node;
    friend List;
};

template<typename T>
struct List<T>::Node : public Link
{
    Node(const T& v) : value(v) { /**/ }
    T value;
};

template<typename T>
List<T>::List() : size_(0)
{
    sentinel_ = new Link;
}

template<typename T>
List<T>::~List()
{
    clear();
    delete sentinel_;
}

template<typename T>
void List<T>::push_back(const T& t)
{
    Node* n = new Node(t);
    n->next = sentinel_;
    n->prev = sentinel_->prev;
    sentinel_->prev->next = n;
    sentinel_->prev = n;
    ++size_;
}

template<typename T>
void List<T>::push_front(const T& t)
{
    Node* n = new Node(t);
    n->next = sentinel_->next;
    n->prev = sentinel_;
    sentinel_->next->prev = n;
    sentinel_->next = n;
    ++size_;
}

template<typename T>
void List<T>::pop_back()
{
    Link* tmp = sentinel_->prev;
    unlink(tmp);
    delete tmp;
    --size_;
}

template<typename T>
void List<T>::pop_front()
{
    Link* tmp = sentinel_->next;
    unlink(tmp);
    delete tmp;
    --size_;
}

template<typename T>
void List<T>::clear()
{
    while (!empty())
    {
        pop_back();
    }
}

template<typename T>
T& List<T>::get(const T& t) const
{
    for (Iterator it = begin(); it != end(); ++it)
    {
        if (*it == t) return *it;
    }
}

template<typename T>
typename List<T>::Iterator List<T>::erase(Iterator it)
{
    Node* prev = it.node;
    unlink(it.node);
    delete it.node;
    return Iterator(prev);
}

template<typename T>
T& List<T>::front() const
{
    Node* n = static_cast<Node*>(sentinel_->next);
    return n->value;
}

template<typename T>
T& List<T>::back() const
{
    Node* n = static_cast<Node*>(sentinel_->prev);
    return n->value;
}

template<typename T>
int List<T>::size() const
{
    return size_;
}

template<typename T>
bool List<T>::empty() const
{
    return size_ == 0;
}

template<typename T>
typename List<T>::Iterator List<T>::begin()
{
    return Iterator(static_cast<Node*>(sentinel_->next));
}

template<typename T>
typename List<T>::Iterator List<T>::end()
{
    return Iterator(static_cast<Node*>(sentinel_));
}

template<typename T>
void List<T>::unlink(Link* l)
{
    l->next->prev = l->prev;
    l->prev->next = l->next;
    l->next = l;
    l->prev = l;
}
#pragma once

#include "List.h"

template<typename T>
class Graph
{
public:
    ~Graph();

    void print();

    void add_vertex(const T&);
    void remove_vertex(const T&);

    //virtual void add_edge(const T&, const T&, int weight) = 0;
    //virtual void remove_edge(const T&, const T&) = 0;
protected:
    struct Vertex;
    struct Edge;
private:
    List<Vertex*> graph_;
};

template<typename T>
struct Graph<T>::Vertex
{
    Vertex(T t) : value(t) {}
    bool operator==(const Vertex& rhs) { return value == rhs.value; }
    T value;
    List<Edge*> in;
    List<Edge*> out;
};

template<typename T>
struct Graph<T>::Edge
{
    Vertex* destination;
    int weight;
};

template<typename T>
Graph<T>::~Graph()
{
    // TODO:
}

template<typename T>
void Graph<T>::print()
{
    for (Vertex* v : graph_)
    {
        std::cout << v->value << " -> ";
        std::cout << "[in = {";
        for (Edge* e : v->in)
        {
            std::cout << ' ' << e->destination;
        }
        std::cout << " }], ";
        std::cout << "[out = {";
        for (Edge* e : v->out)
        {
            std::cout << ' ' << e->destination;
        }
        std::cout << " }]";
        std::cout << std::endl;
    }
}

template<typename T>
void Graph<T>::add_vertex(const T& t)
{
    Vertex* v = new Vertex(t);
    graph_.push_back(v);
}

template<typename T>
void Graph<T>::remove_vertex(const T& t)
{
    Vertex* v = graph_.get(t);
    while (!v->out.empty())
    {
        Edge* e = v->out.back();
        Vertex* dest = e->destination;
        auto it = dest->in.begin();
        while (it != dest->in.end())
        {
            Edge* e = *it;
            Vertex* w = dest;
            if (v == w)
                break;
            ++it;
        }
        dest->in.erase(it);
        v->out.pop_back();
    }
    while (!v->in.empty())
    {
        Edge* e = v->in.back();
        Vertex* dest = e->destination;

        auto it = dest->out.begin();
        while (it != dest->out.end())
        {
            Edge* e = *it;
            Vertex* w = e->destination;
            if (v == w)
                break;
            ++it;
        }
        dest->out.erase(it);
        v->in.pop_back();
    }
}
#pragma一次
模板
班级名单
{
公众:
结构迭代器;
List();
~List();
无效推回(常数T&);
无效前推(常数&);
void pop_back();
void pop_front();
无效清除();
迭代器擦除(迭代器擦除);
T&front()常数;
T&back()常数;
T&get(const T&)const;
int size()常量;
bool empty()常量;
迭代器begin();
迭代器end();
受保护的:
结构链接;
结构节点;
无效取消链接(链接*);
链接*哨兵;
int-size_389;;
};
模板
结构列表::链接
{
Link():next(this),prev(this){/**/}
链接*下一步;
链接*prev;
};
模板
结构列表::迭代器
{
迭代器()=默认值;
迭代器(Node*n):节点(n){}
运算符*(){return node->value;}
布尔运算符==(常量迭代器&rhs){return(node->value==rhs.node->value);}
布尔运算符!=(常量迭代器&rhs){return(node->value!=rhs.node->value);}
迭代器和运算符++()
{ 
节点=静态_cast(节点->下一步);
归还*这个;
}
私人:
节点*节点;
朋友名单;
};
模板
结构列表::节点:公共链接
{
节点(常数T&v):值(v){/**/}
T值;
};
模板
列表::列表():大小(0)
{
哨兵=新链接;
}
模板
列表::~List()
{
清除();
删除哨兵;
}
模板
无效列表::推回(常量T&T)
{
节点*n=新节点(t);
n->next=哨兵;
n->prev=哨兵->prev;
哨兵->上一个->下一个=n;
哨兵->前卫=n;
++大小;
}
模板
无效列表::推前(常量T&T)
{
节点*n=新节点(t);
n->next=哨兵\->next;
n->prev=哨兵;
哨兵->下一步->上一步=n;
哨兵->下一步=n;
++大小;
}
模板
无效列表::pop_back()
{
Link*tmp=sentinel_uu->prev;
解除链接(tmp);
删除tmp;
--大小;
}
模板
无效列表::pop_front()
{
Link*tmp=sentinel->next;
解除链接(tmp);
删除tmp;
--大小;
}
模板
无效列表::清除()
{
而(!empty())
{
向后弹出();
}
}
模板
T&List::get(const T&T)const
{
for(迭代器it=begin();it!=end();++it)
{
如果(*it==t),则返回*it;
}
}
模板
typename列表::迭代器列表::擦除(迭代器)
{
Node*prev=it.Node;
取消链接(it.node);
删除它。节点;
返回迭代器(prev);
}
模板
T&List::front()常量
{
节点*n=静态\u投射(哨兵\u->next);
返回n->value;
}
模板
T&List::back()常量
{
节点*n=静态施法(哨兵->上一步);
返回n->value;
}
模板
int List::size()常量
{
返回大小;
}
模板
bool List::empty()常量
{
返回大小==0;
}
模板
typename列表::迭代器列表::begin()
{
返回迭代器(static_cast(sentinel_->next));
}
模板
typename列表::迭代器列表::end()
{
返回迭代器(static_cast(sentinel_));
}
模板
无效列表::取消链接(链接*l)
{
l->next->prev=l->prev;
l->prev->next=l->next;
l->next=l;
l->prev=l;
}

图形.h

#pragma once

template<typename T>
class List
{
public:
    struct Iterator;

    List();
    ~List();

    void push_back(const T&);
    void push_front(const T&);
    void pop_back();
    void pop_front();
    void clear();
    Iterator erase(Iterator it);

    T& front() const;
    T& back() const;
    T& get(const T&) const;
    int size() const;
    bool empty() const;
    Iterator begin();
    Iterator end();
protected:
    struct Link;
    struct Node;

    void unlink(Link*);

    Link* sentinel_;
    int size_;
};

template<typename T>
struct List<T>::Link
{
    Link() : next(this), prev(this) { /**/ }
    Link* next;
    Link* prev;
};

template<typename T>
struct List<T>::Iterator
{
    Iterator() = default;
    Iterator(Node* n) : node(n) {}
    T& operator*() { return node->value; }
    bool operator==(const Iterator& rhs) { return (node->value == rhs.node->value); }
    bool operator!=(const Iterator& rhs) { return (node->value != rhs.node->value); }
    Iterator& operator++()
    { 
        node = static_cast<Node*>(node->next);
        return *this;
    }
private:
    Node* node;
    friend List;
};

template<typename T>
struct List<T>::Node : public Link
{
    Node(const T& v) : value(v) { /**/ }
    T value;
};

template<typename T>
List<T>::List() : size_(0)
{
    sentinel_ = new Link;
}

template<typename T>
List<T>::~List()
{
    clear();
    delete sentinel_;
}

template<typename T>
void List<T>::push_back(const T& t)
{
    Node* n = new Node(t);
    n->next = sentinel_;
    n->prev = sentinel_->prev;
    sentinel_->prev->next = n;
    sentinel_->prev = n;
    ++size_;
}

template<typename T>
void List<T>::push_front(const T& t)
{
    Node* n = new Node(t);
    n->next = sentinel_->next;
    n->prev = sentinel_;
    sentinel_->next->prev = n;
    sentinel_->next = n;
    ++size_;
}

template<typename T>
void List<T>::pop_back()
{
    Link* tmp = sentinel_->prev;
    unlink(tmp);
    delete tmp;
    --size_;
}

template<typename T>
void List<T>::pop_front()
{
    Link* tmp = sentinel_->next;
    unlink(tmp);
    delete tmp;
    --size_;
}

template<typename T>
void List<T>::clear()
{
    while (!empty())
    {
        pop_back();
    }
}

template<typename T>
T& List<T>::get(const T& t) const
{
    for (Iterator it = begin(); it != end(); ++it)
    {
        if (*it == t) return *it;
    }
}

template<typename T>
typename List<T>::Iterator List<T>::erase(Iterator it)
{
    Node* prev = it.node;
    unlink(it.node);
    delete it.node;
    return Iterator(prev);
}

template<typename T>
T& List<T>::front() const
{
    Node* n = static_cast<Node*>(sentinel_->next);
    return n->value;
}

template<typename T>
T& List<T>::back() const
{
    Node* n = static_cast<Node*>(sentinel_->prev);
    return n->value;
}

template<typename T>
int List<T>::size() const
{
    return size_;
}

template<typename T>
bool List<T>::empty() const
{
    return size_ == 0;
}

template<typename T>
typename List<T>::Iterator List<T>::begin()
{
    return Iterator(static_cast<Node*>(sentinel_->next));
}

template<typename T>
typename List<T>::Iterator List<T>::end()
{
    return Iterator(static_cast<Node*>(sentinel_));
}

template<typename T>
void List<T>::unlink(Link* l)
{
    l->next->prev = l->prev;
    l->prev->next = l->next;
    l->next = l;
    l->prev = l;
}
#pragma once

#include "List.h"

template<typename T>
class Graph
{
public:
    ~Graph();

    void print();

    void add_vertex(const T&);
    void remove_vertex(const T&);

    //virtual void add_edge(const T&, const T&, int weight) = 0;
    //virtual void remove_edge(const T&, const T&) = 0;
protected:
    struct Vertex;
    struct Edge;
private:
    List<Vertex*> graph_;
};

template<typename T>
struct Graph<T>::Vertex
{
    Vertex(T t) : value(t) {}
    bool operator==(const Vertex& rhs) { return value == rhs.value; }
    T value;
    List<Edge*> in;
    List<Edge*> out;
};

template<typename T>
struct Graph<T>::Edge
{
    Vertex* destination;
    int weight;
};

template<typename T>
Graph<T>::~Graph()
{
    // TODO:
}

template<typename T>
void Graph<T>::print()
{
    for (Vertex* v : graph_)
    {
        std::cout << v->value << " -> ";
        std::cout << "[in = {";
        for (Edge* e : v->in)
        {
            std::cout << ' ' << e->destination;
        }
        std::cout << " }], ";
        std::cout << "[out = {";
        for (Edge* e : v->out)
        {
            std::cout << ' ' << e->destination;
        }
        std::cout << " }]";
        std::cout << std::endl;
    }
}

template<typename T>
void Graph<T>::add_vertex(const T& t)
{
    Vertex* v = new Vertex(t);
    graph_.push_back(v);
}

template<typename T>
void Graph<T>::remove_vertex(const T& t)
{
    Vertex* v = graph_.get(t);
    while (!v->out.empty())
    {
        Edge* e = v->out.back();
        Vertex* dest = e->destination;
        auto it = dest->in.begin();
        while (it != dest->in.end())
        {
            Edge* e = *it;
            Vertex* w = dest;
            if (v == w)
                break;
            ++it;
        }
        dest->in.erase(it);
        v->out.pop_back();
    }
    while (!v->in.empty())
    {
        Edge* e = v->in.back();
        Vertex* dest = e->destination;

        auto it = dest->out.begin();
        while (it != dest->out.end())
        {
            Edge* e = *it;
            Vertex* w = e->destination;
            if (v == w)
                break;
            ++it;
        }
        dest->out.erase(it);
        v->in.pop_back();
    }
}
#pragma一次
#包括“List.h”
模板
类图
{
公众:
~Graph();
作废打印();
void add_顶点(常数T&);
无效删除顶点(常数T&);
//虚空添加_边(常数T&,常数T&,整数权重)=0;
//虚空删除_边(常数T&,常数T&)=0;
受保护的:
结构顶点;
结构边缘;
私人:
列表图;
};
模板
结构图::顶点
{
顶点(T):值(T){}
布尔运算符==(常量顶点和rhs){返回值==rhs.value;}
T值;
列入;
列出;
};
模板
结构图::边
{
顶点*目的地;
整数权重;
};
模板
图::~Graph()
{
//待办事项:
}
模板
void图形::print()
{
对于(顶点*v:图_u2;)
{
标准::清除(it)中的cout值;
v->out.pop_back();
}
而(!v->in.empty())
{
边缘*e=v->in.back();
顶点*dest=e->destination;
auto it=dest->out.begin();
while(it!=dest->out.end())
{
边*e=*it;
顶点*w=e->目的地;
如果(v==w)
打破
++它;
}
dest->out.擦除(它);
v->in.pop_back();
}
}

您似乎以某种方式混淆了
图形
模板及其包含的
列表
类的模板参数

当你打电话的时候

Vertex* v = graph_.get(t);
t
的类型可能不是
Vertex*
。但是您可以调用
List::get(Vertex*&)
t
的类型与编译器告诉您的
Vertex*
不匹配(但是您也没有显示完整的错误消息,应该有更多信息,例如关于模板类型的信息)


确切的解决方法很难说,因为您的问题中没有足够的信息。

您似乎不知何故混淆了
图形的模板参数及其包含的
列表

当你打电话的时候

Vertex* v = graph_.get(t);
t
的类型可能不是
Vertex*
。但是您可以调用
List::get(Vertex*&)
t
的类型与编译器告诉您的
Vertex*
不匹配(但是您也没有显示完整的错误消息,应该有更多信息,例如关于模板类型的信息)

确切的解决方法很难说,