交换类的对象

时间:2019-05-19 15:58:25

标签: c++ vector swap

我有一个哈希表类,我试图交换哈希表的对象并反复出错

[Error] no matching function for call to 'swap(hashmap*, hashmap*)'
[Note] void swap(hashmap&, hashmap&)
[Note] no known conversion for argument 1 from 'hashmap*' to 'hashmap&'

我已经在堆栈溢出时搜索了类似的问题,但发现了类似的问题,但答案并不令人满意。

class hashmap{

    public:
        int key;
        int value;   //Include a Deconstructor

};

void swap(hashmap &k1,hashmap &k2)
{
    hashmap x;
    x=k1;
    k1=k2;
    k2=x;
}

1 个答案:

答案 0 :(得分:1)

您没有显示如何调用交换功能。 看来您正在尝试调用以下函数:

hashmap a;
hashmap b;
swap(&a, &b);

但是swap函数需要引用:

swap(a,b);