当前位置: 首页 > 知识库问答 >
问题:

在指针数组中的linkedlist中创建对象时出现编译错误:C++

谭宜
2023-03-14

***更新****

所以首先我要尝试哈希。为了使其简短,我创建了一个linkedlsit类,它接受一个泛型参数。我有一个哈希表类,我试图在其中创建(我相信)一个linkedlist指针数组(请记住,linkedlist采用泛型类型)

因此,在我的哈希表类中,我有一个私有变量

SLL< Entry <string, int> >** list;

其中SLL是我的链表,Entry是保存键(字符串)和值(int)的对象,并且绑定使其成为指针数组。

在哈希表构造函数中,我这样创建它

list = new SLL<Entry<string, int> > * [this->size]; 

现在在我的代码中,我尝试在hashcode函数结束后将Entry对象追加到数组中

list[hash]->append(new Entry<string, int>(key, e));

但是它会出现这个错误

HashTable.h: In member function 'void HashTable::createEntry(std::string, int)':
HashTable.h:78:53: error: no matching function for call to 'SLL<Entry<std::basic_string<char>, int> >::append(Entry<std::basic_string<char>, int>*)'
list[hash]->append(new Entry<string, int>(key, i));

如果我将Entry替换为linkedlist中的jsut(int)或float(float)或甚至string(字符串)中的对象,那么它就会起作用

那么是什么导致了这种情况呢?请并感谢您,如果您需要更多信息,请告诉我:)

#ifndef SLL_H
#define SLL_H

 template <class T>
class SLL 
{
private:
    Node<T>* head;
    Node<T>* tail;
    int size;

public:
    SLL();
    virtual ~SLL();
    void append(T&);
    void append(T*);
    void prepend(T);
    void deleteElem(int);
    void toString();
    int getSize();
    void insertAt(T, int);
    T retrieveDataAt(int);
 };
 #endif /* SLL_H */

 template <class T>
SLL<T>::SLL() 
 {
this->tail = NULL;
this->head = NULL;
this->size = 0;
}
void SLL<T>::append(T data)
{
//do stuff
        this->head = new Node<T>(data);;
 }

共有1个答案

黄成荫
2023-03-14

您发布的代码有几个问题,它只是表明,在使用模板时,您需要确保所有内容都匹配得很好。特别是因为编译器甚至不会关心某些类型的错误,直到您实际使用某种类型实例化模板。

第一个是您的类sll 声明了许多成员函数,其中两个是sll::append(t&)sll::append(t*)。问题是,在您发布的示例代码中,您正在定义的成员函数是sll::append(T),它并不存在!

第二个是因为new返回一个指向类型的指针,所以您的代码:

list[hash]->append(new Entry<string, int>(key, e));

相当于

Entry<string, int>* data_ptr = new Entry<string, int>(key, e);
list[hash]->append(data_ptr);

它将查找格式为sll::append(T*)的成员函数,而不是sll::append(T)的成员函数,并且没有定义这样的函数!

这里有一些最低限度的工作代码,应该为您编译。请注意,为了简洁起见,我使用了std::pair而不是entry,您需要使用-std=C++11或等效标志(例如g++-std=C++11main.cpp)进行编译,因为我使用了nullptr:

#include <utility>
#include <string>

template<class T>
class SLL;

// singly linked list node
template<class T>
class Node
{
private:
    Node<T> *next;
    T data;

    friend class SLL<T>;
public:
    Node(T input) : next(nullptr),
        data(input) {}
    ~Node() {delete next;}
};

// the singly linked list class
template <class T>
class SLL
{
private:
    Node<T>* head;
    Node<T>* tail;
    std::size_t size;

public:
    SLL() : head(nullptr),
        tail(nullptr), size(0) {}
    ~SLL() {delete head;}

    std::size_t getSize() const {
        return size;}
    void append(T data);
};

template<class T>
void SLL<T>::append(T data)
{
    Node<T> *temp = new Node<T>(data);
    if (!head)
        head = temp;

    if (tail)
        tail->next = temp;
    tail = temp;

    size += 1;
}

int main()
{
    // less typing
    using list_type = SLL<std::pair<std::string, int>>;

    // allocation for the list of lists
    std::size_t hash_size = 10;
    list_type** list_of_lists = new list_type*[hash_size]();

    // data to input
    std::string key = "key";
    int value = 9330323;

    std::size_t hash = 4;

    // check and append
    if (!list_of_lists[hash])
        list_of_lists[hash] = new list_type;

    list_of_lists[hash]->append(std::pair<std::string, int>(key, value));

    // cleanup
    for (std::size_t i = 0; i < hash_size; ++i)
        delete list_of_lists[i];
    delete[] list_of_lists;
}
 类似资料:
  • 我试图创建这个对象,但我得到了一个编译器错误,但我没有意义,因为整数扩展了数字,所以它应该可以工作。

  • 问题内容: 我一直试图创建一个包含两个值的类的数组,但是当我尝试将值应用于该数组时,我得到了。 为什么会出现此异常,我该如何解决? 问题答案: 你创建了数组,但未在其中放置任何内容,因此你有一个包含5个元素的数组,所有元素均为null。你可以添加 在设置的行之前。

  • 我正在编写一些为数据库创建表的PL/SQL代码。 当我尝试跟踪代码时,我一直收到错误PLS-00103。我查看了其他线程,它表明缺少循环的开始,但我看不到可能在哪里。 设置服务器输出 执行THELO; 问题: PLS-00103:在预期以下情况之一时遇到符号“CREATE”:(begin case declare end exception exit for goto if loop mod nu

  • 问题内容: 行给出了编译错误。 为何不允许这样做的任何特定原因?如何使用数组常量初始化String数组? 编辑:谢谢大家的回答。现在,我很清楚什么是允许的,什么是不允许的。但是我能问你 为什么 不允许这样做吗? 仔细搜索一下之后,我发现了这个链接,在其中,被告知像这样的编码使编译器不明确- 宠物应该是String数组还是Objects数组。但是,从声明中可以很好地看出它是一个String数组,对吗

  • 问题内容: 我正在研究一个稀疏矩阵类,该类需要使用数组来存储矩阵的值。数组的每个元素(即每个)代表矩阵的一行。并且,数组中的每个元素代表一列和存储的值。 在我的课程中,我将数组声明为: 并且,在的构造函数中SparseMatrix,我尝试定义: 我最终得到的错误是 无法创建的通用数组 因此,我有两个问题: 我做错了什么 如果无法创建数组,为什么在数组的声明中可以接受该类型? 是我创建的课程。而且,

  • 我需要创建一个对象(银行),其中包含一组客户端和bankID。我的问题是,我不知道如何在主函数中创建银行。 银行类别: 客户端类: 主要类别: 这些是问题所在: 你必须创建一个程序来模拟银行活动。该系统包括以下模块:银行—客户(客户数组)— idBank(字符串)5 BancAccount — accountNumber(字符串)—金额(浮点)客户—姓名(字符串)—地址(字符串)—账户(银行账户数