错误 1 error C2679: 二进制“<<”: 没有找到接受“void”类型的右操作数的运算符(或没有可接受的转换)

 void Get(const Key & k)
    {
        for (size_t i = 0; i < currentSize; ++i)
        {
            if (arr[i].key == k)
                return arr[i].value;
        }
        return Value();

    }

在主程序中输出时出现没有找到“void”类型的右操作数的运算符(没有可接受的转换))

cout << lm.Get("Tom") << endl;

Get中的参数是string类型,输出的是数值型。“<<”默认输出的是整数类型,再返回Get函数,是有返回值的,返回值为Value,而出错的问题就在于将Get函数设置成了void类型,下面给出改写后的。

非常简单

Value Get(const Key & k)
    {
        for (size_t i = 0; i < currentSize; ++i)
        {
            if (arr[i].key == k)
                return arr[i].value;
        }
        return Value();

    }

运行成功。

#include<iostream>
#include<List>
using namespace std;

class Vertex
{
public:
    char Label;
    Vertex(char lab){ Label = lab; }
};
//输出顶点是自己定义的
//没有对如何使用输出操作符来显示顶点,用运算符重载来解决
//下面是运算符重载部分代码
ostream& operator<<(ostream & out, const Vertex& v)
{
    cout << v.Label;
    return out;
}
//上面是运算符重载代码
template<class T>
class Graph
{
    public:

    Graph(const int vertices) :n(vertices)
    {
        VertexList = new T*[n];//保存顶点
        HeadNodes = new list<int>[n];//保存链表
        nVerts = 0;

    }
    ~Graph()
    {
        delete []VertexList;
        delete[]HeadNodes;

    }
    void addVertex(T* v);
    void addEdge(int start, int end);
    void printVertice();
    void printAdjList();

private:
    T**VertexList;
    list<int>*HeadNodes;
    int n;
    int nVerts;

};
template<class T>
void Graph<T>::addVertex(T* v)
{
    VertexList[nVerts++] = v;

}

template<class T>
void Graph<T>::addEdge(int start, int end)
{
    HeadNodes[start].push_back(end);
}
template<class T>
void Graph<T>::printVertice()
{
    for (int i = 0; i < nVerts; i++)
        cout << *VertexList[i] << " ";
    cout << endl;

}

template<class T>
void Graph<T>::printAdjList()
{
    for (int i = 0; i < nVerts; i++)
    {
        cout << i << "->";
        for (list<int>::iterator iter = HeadNodes[i].begin(); iter != HeadNodes[i].end(); ++iter)
            cout << *iter << "->";
        cout <<"end"<< endl;

    }
}

int main()
{

    //Graph<char> g(5);
    /*char a = 'A';
    char b = 'B';
    char c = 'C';
    char d = 'D';
    char e = 'E';*/

    Graph<Vertex>g(5);
    Vertex a('A');
    Vertex b('B');
    Vertex c('C');
    Vertex d('D');
    Vertex e('E');

    g.addVertex(&a);
    g.addVertex(&b);
    g.addVertex(&c);
    g.addVertex(&d);
    g.addVertex(&e);
    g.printVertice();

    g.addEdge(0, 1);
    g.addEdge(0, 3);
    g.addEdge(1, 0);
    g.addEdge(1, 4);
    g.addEdge(2, 4);
    g.addEdge(3, 0);
    g.addEdge(3, 4);
    g.addEdge(4, 1);
    g.addEdge(4, 2);
    g.addEdge(4, 3);

    g.printAdjList();

    system("pause");
    return 0;
}

还是那句,当出现这种错误时,是因为c++ 不知道如何处理用户自己定义的变量,需要做运算符重载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值