Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用自定义类的向量复制构造函数不起作用?_C++_C++11_Linked List_Constants_Copy Constructor - Fatal编程技术网

C++ 使用自定义类的向量复制构造函数不起作用?

C++ 使用自定义类的向量复制构造函数不起作用?,c++,c++11,linked-list,constants,copy-constructor,C++,C++11,Linked List,Constants,Copy Constructor,我试图通过LinkedList为冲突实现哈希表,但在哈希表内部实现复制构造函数时遇到问题(与常量正确性的东西相混淆) 这就是我看到的错误: void LinkedList<KVP<T>>::add(const KVP<T> &)' : cannot convert argument 1 from 'const int' to 'const KVP<T> & void LinkedList::add(const KVP&):无法将参

我试图通过LinkedList为冲突实现哈希表,但在哈希表内部实现复制构造函数时遇到问题(与常量正确性的东西相混淆)

这就是我看到的错误:

void LinkedList<KVP<T>>::add(const KVP<T> &)' : cannot convert argument 1 from 'const int' to 'const KVP<T> &
void LinkedList::add(const KVP&):无法将参数1从“const int”转换为“const KVP”&
这是我的密码:

哈希表:

#include <stdlib.h>
#include <string>
#include <vector>
#include "LinkedList.h"
#pragma once

template <typename T>
struct KVP
{
    KVP() {}
    KVP(const T &data, const std::string &key) : data(data), key(key) {}
    const std::string key;
    T data;
};

template <typename T>
class Hashtable
{
    typedef KVP<T> kvp;
    typedef LinkedList<kvp> list;

    std::vector<list> table;
    std::size_t size;

public:

    Hashtable(int size) : size(size), table(size) {}
    Hashtable(const Hashtable& other) : size(other.size) 
    {
        table = other.table; //this causes problems
    }
    Hashtable& operator=(Hashtable other)
    {
        swap(*this, other);
        return *this;
    }
    ...  
};
#include <stdlib.h>
#include <memory>

#pragma once

template <typename T>
class LinkedList;

template <typename TNode>
class LinkedListIterator
{
    friend class LinkedList<typename TNode::value_type>;
    TNode* p;
public:
    LinkedListIterator(TNode* p) : p(p) {}
    LinkedListIterator(const LinkedListIterator& other) : p(other.p) {}
    LinkedListIterator& operator=(LinkedListIterator other) { std::swap(p, other.p); return *this; }
    void operator++() { p = p->next; }
    void operator++(int) { p = p->next; }
    bool operator==(const LinkedListIterator& other) { return p == other.p; }
    bool operator!=(const LinkedListIterator& other) { return !(p == other.p); }
    const int& operator*() const { return p->data; }
    LinkedListIterator<TNode> operator+(int i)
    {
        LinkedListIterator<TNode> iter = *this;
        while (i-- > 0 && iter.p)
        {
            ++iter;
        }
        return iter;
    }
};

template <typename T>
class Node
{
    friend class LinkedList<T>;
    friend class LinkedListIterator<Node<T>>;
    friend class LinkedListIterator<const Node<T>>;

    Node() : next(nullptr) {}
    Node(const T &data) : data(data), next(nullptr) {}
    Node<T> *next;
    T data;
public:
    typedef T value_type;
};

template <typename T>
class LinkedList
{
    typedef Node<T> node;

    std::size_t size;
    std::unique_ptr<node> head;
    std::unique_ptr<node> tail;

    void init()
    {
        size = 0;
        head.reset(new node);
        tail.reset(new node);
        head->next = tail.get();
    }

public:
    typedef LinkedListIterator<node> iterator;
    typedef LinkedListIterator<const node> const_iterator;

    LinkedList() { init(); }

    LinkedList(const LinkedList& other)
    {
        init();
        const_iterator i = other.begin();
        while (i != other.end())
        {
            add(*i);
            i++;
        }

        head.reset(other.head.get());
        tail.reset(other.tail.get());
    }

    LinkedList(LinkedList&& other)
    {
        size = other.size;
        head = other.head;
        tail = other.tail;
        other.size = 0;
    }

    LinkedList& operator=(LinkedList other)
    {
        swap(*this, other);
        return *this;
    }

    LinkedList& operator=(LinkedList&& other)
    {
        assert(this != &other);     
        while (head->next != tail)
            remove(begin());
        head = other.head;
        tail = other.tail;
        size = other.size;
        other.size = 0;
        return *this;
    }

    virtual ~LinkedList()
    {
        while (head->next != tail.get())
            remove(begin());
    }

    friend void swap(LinkedList& first, LinkedList& second)
    {
        std::swap(first.size, second.size);
        std::swap(first.head, second.head);
        std::swap(first.tail, second.tail);
    }

    void add(const T &value)
    {
        node *first = new node(value);
        first->next = head->next;
        head->next = first;
        size++;
    }

    void remove(iterator& removeIter)
    {
        node *last = head.get();
        iterator i = begin();

        while (i != removeIter)
        {
            last = i.p;
            ++i;
        }

        if (i != end())
        {
            last->next = i.p->next;
            size--;
            delete i.p;
        }
    }

    const int getSize() 
    { 
        return size;
    }

    iterator begin() 
    {
        return iterator(head->next);
    }

    const_iterator begin() const
    {
        return const_iterator(head->next);
    }

    iterator end()
    {
        return iterator(tail.get());
    }

    const_iterator end() const
    {
        return const_iterator(tail.get());
    }
};
#包括
#包括
#包括
#包括“LinkedList.h”
#布拉格语一次
模板
结构KVP
{
KVP(){}
KVP(常量T和数据,常量std::字符串和键):数据(数据),键(键){}
常量std::字符串键;
T数据;
};
模板
类哈希表
{
typedef-KVP-KVP;
typedef链接列表;
std::向量表;
标准:大小;
公众:
哈希表(int size):大小(size),表(size){}
哈希表(常量哈希表和其他):大小(其他.size)
{
table=other.table;//这会导致问题
}
哈希表和运算符=(哈希表其他)
{
掉期(*本,其他);
归还*这个;
}
...  
};
链接列表:

#include <stdlib.h>
#include <string>
#include <vector>
#include "LinkedList.h"
#pragma once

template <typename T>
struct KVP
{
    KVP() {}
    KVP(const T &data, const std::string &key) : data(data), key(key) {}
    const std::string key;
    T data;
};

template <typename T>
class Hashtable
{
    typedef KVP<T> kvp;
    typedef LinkedList<kvp> list;

    std::vector<list> table;
    std::size_t size;

public:

    Hashtable(int size) : size(size), table(size) {}
    Hashtable(const Hashtable& other) : size(other.size) 
    {
        table = other.table; //this causes problems
    }
    Hashtable& operator=(Hashtable other)
    {
        swap(*this, other);
        return *this;
    }
    ...  
};
#include <stdlib.h>
#include <memory>

#pragma once

template <typename T>
class LinkedList;

template <typename TNode>
class LinkedListIterator
{
    friend class LinkedList<typename TNode::value_type>;
    TNode* p;
public:
    LinkedListIterator(TNode* p) : p(p) {}
    LinkedListIterator(const LinkedListIterator& other) : p(other.p) {}
    LinkedListIterator& operator=(LinkedListIterator other) { std::swap(p, other.p); return *this; }
    void operator++() { p = p->next; }
    void operator++(int) { p = p->next; }
    bool operator==(const LinkedListIterator& other) { return p == other.p; }
    bool operator!=(const LinkedListIterator& other) { return !(p == other.p); }
    const int& operator*() const { return p->data; }
    LinkedListIterator<TNode> operator+(int i)
    {
        LinkedListIterator<TNode> iter = *this;
        while (i-- > 0 && iter.p)
        {
            ++iter;
        }
        return iter;
    }
};

template <typename T>
class Node
{
    friend class LinkedList<T>;
    friend class LinkedListIterator<Node<T>>;
    friend class LinkedListIterator<const Node<T>>;

    Node() : next(nullptr) {}
    Node(const T &data) : data(data), next(nullptr) {}
    Node<T> *next;
    T data;
public:
    typedef T value_type;
};

template <typename T>
class LinkedList
{
    typedef Node<T> node;

    std::size_t size;
    std::unique_ptr<node> head;
    std::unique_ptr<node> tail;

    void init()
    {
        size = 0;
        head.reset(new node);
        tail.reset(new node);
        head->next = tail.get();
    }

public:
    typedef LinkedListIterator<node> iterator;
    typedef LinkedListIterator<const node> const_iterator;

    LinkedList() { init(); }

    LinkedList(const LinkedList& other)
    {
        init();
        const_iterator i = other.begin();
        while (i != other.end())
        {
            add(*i);
            i++;
        }

        head.reset(other.head.get());
        tail.reset(other.tail.get());
    }

    LinkedList(LinkedList&& other)
    {
        size = other.size;
        head = other.head;
        tail = other.tail;
        other.size = 0;
    }

    LinkedList& operator=(LinkedList other)
    {
        swap(*this, other);
        return *this;
    }

    LinkedList& operator=(LinkedList&& other)
    {
        assert(this != &other);     
        while (head->next != tail)
            remove(begin());
        head = other.head;
        tail = other.tail;
        size = other.size;
        other.size = 0;
        return *this;
    }

    virtual ~LinkedList()
    {
        while (head->next != tail.get())
            remove(begin());
    }

    friend void swap(LinkedList& first, LinkedList& second)
    {
        std::swap(first.size, second.size);
        std::swap(first.head, second.head);
        std::swap(first.tail, second.tail);
    }

    void add(const T &value)
    {
        node *first = new node(value);
        first->next = head->next;
        head->next = first;
        size++;
    }

    void remove(iterator& removeIter)
    {
        node *last = head.get();
        iterator i = begin();

        while (i != removeIter)
        {
            last = i.p;
            ++i;
        }

        if (i != end())
        {
            last->next = i.p->next;
            size--;
            delete i.p;
        }
    }

    const int getSize() 
    { 
        return size;
    }

    iterator begin() 
    {
        return iterator(head->next);
    }

    const_iterator begin() const
    {
        return const_iterator(head->next);
    }

    iterator end()
    {
        return iterator(tail.get());
    }

    const_iterator end() const
    {
        return const_iterator(tail.get());
    }
};
#包括
#包括
#布拉格语一次
模板
类链接列表;
模板
类链接标识符
{
朋友类链接列表;
TNode*p;
公众:
链接标识符(TNode*p):p(p){}
LinkedListIterator(const LinkedListIterator&other):p(other.p){}
LinkedListIterator&operator=(LinkedListIterator其他){std::swap(p,other.p);返回*this;}
void操作符++(){p=p->next;}
void操作符++(int){p=p->next;}
bool运算符==(const LinkedListIterator&other){return p==other.p;}
bool操作符!=(constlinkedlistiterator&other){return!(p==other.p);}
常量int&运算符*()常量{return p->data;}
LinkedListIterator运算符+(int i)
{
LinkedListInterator iter=*此;
而(i-->0&&iter.p)
{
++iter;
}
返回iter;
}
};
模板
类节点
{
朋友类链接列表;
好友类链接标识符;
好友类链接标识符;
Node():next(nullptr){}
节点(const T&data):数据(data),下一个(nullptr){
节点*下一步;
T数据;
公众:
类型定义T值_类型;
};
模板
类链接列表
{
typedef节点;
标准:大小;
std::唯一的ptr头;
std::唯一的_ptr尾;
void init()
{
尺寸=0;
复位(新节点);
tail.reset(新节点);
head->next=tail.get();
}
公众:
typedef LinkedList迭代器;
typedef LinkedListIterator常量迭代器;
LinkedList(){init();}
LinkedList(常量LinkedList和其他)
{
init();
常量迭代器i=other.begin();
而(i!=other.end())
{
添加(*i);
i++;
}
head.reset(other.head.get());
tail.reset(other.tail.get());
}
LinkedList(LinkedList和其他)
{
尺寸=其他尺寸;
头=其他头;
tail=other.tail;
其他尺寸=0;
}
LinkedList和operator=(LinkedList其他)
{
掉期(*本,其他);
归还*这个;
}
LinkedList和operator=(LinkedList和其他)
{
断言(此!=&其他);
while(头部->下一步!=尾部)
删除(begin());
头=其他头;
tail=other.tail;
尺寸=其他尺寸;
其他尺寸=0;
归还*这个;
}
虚拟~LinkedList()
{
while(head->next!=tail.get())
删除(begin());
}
好友无效交换(LinkedList和first,LinkedList和second)
{
标准::交换(第一个大小,第二个大小);
标准::交换(第一个头部,第二个头部);
std::swap(first.tail,second.tail);
}
无效添加(常量T和值)
{
节点*第一个=新节点(值);
第一个->下一个=头部->下一个;
头->下一个=第一个;
大小++;
}
无效删除(迭代器和删除器)
{
node*last=head.get();
迭代器i=begin();
而(i!=删除)
{
last=i.p;
++一,;
}
如果(i!=end())
{
最后->下一步=i.p->下一步;
大小--;
删除i.p;
}
}
常量int getSize()
{ 
返回大小;
}
迭代器begin()
{
返回迭代器(head->next);
}
常量迭代器begin()常量
{
返回常量迭代器(head->next);
}
迭代器结束()
{
返回迭代器(tail.get());
}
常量迭代器end()常量
{
返回常量迭代器(tail.get());
}
};
在其自己的LinkedList副本上,ctor似乎起作用,例如,它编译:

LinkedList<int> list;
LinkedList<int> list2;
list2 = list;
LinkedList列表;
链接列表2;
list2=列表;
但这并不是:

Hashtable<int> table1(50);
Hashtable<int> table2 = table1;
哈希表表1(50);
哈希表2=表1;
编辑:如果我使用指针定义表,也可以:

std::vector<list*> table; 
std::向量表;

它可以工作,但我认为这不是最好的方法。

您的
LinkedListInterator::operator*
返回
常量int&
,而不是
LinkedList的值类型。您的
LinkedList
测试通过,因为模板参数恰好与
运算符*
的硬编码返回类型匹配
常量迭代器是如何定义的?从错误中可以看出,当取消引用时,它似乎返回了一个
int
。该错误可能会产生误导,但如果不知道该错误来自何处,则很难判断。执行此操作时,您需要为
HashTable
定义赋值运算符:
HashTable 2=table1。如果要为此操作使用复制构造函数,请按以下方式编写:
哈希表table2(table1)@djikay如果我调试它实际上是在调用copyctor。哈希表2(表1);给出了完全相同的错误。好的,如果您按如下方式定义复制构造函数呢<代码>哈希表(常量哈希表和其他):s