gpt4 book ai didi

c++ - 无法从 'Node *' 推断出 'int' 的模板参数

转载 作者:行者123 更新时间:2023-11-30 04:45:41 26 4
gpt4 key购买 nike

我目前正在尝试用 C++ 实现 XOR 链表。我尝试使用模板使其通用。编译时会弹出此错误,我无法解决这个问题。

我尝试使用模板在谷歌上搜索 XOR 链表,但到目前为止似乎还没有实现它。

异或链表.h:

#pragma once
#include<iostream>

template <typename T>
struct Node
{
T data;
Node *XORcode;
};

template <typename T>
Node<T> * XOR(struct Node<T> *x, struct Node<T> *y)
{
return (Node<T>*)( (uintptr_t)(x) ^ (uintptr_t)(y) );
}

template <typename T>
class XORlinkedList
{
private:
Node<T> *top, *last;
public:
XORlinkedList()
{
top = last = NULL;
}

void addAtBegin(T data)
{
Node<T> *temp = new Node<T>;
temp->data = data;

temp->XORcode = XOR(top, NULL); //*****ERROR SHOWN IN THIS LINE HERE*****
if(top != NULL)
top->XORcode = XOR(top->XORcode, temp);
top = temp;
}

~XORlinkedList()
{
Node<T> *temp, *storeCode;
temp = top;
storeCode = NULL;

while(top != NULL)
{
temp = top;
top = XOR(top->XORcode, storeCode);
std::cout<<temp->data<<" deleted\n";
storeCode = temp->XORcode;
delete temp;
}
}
};

主要.cpp:

#include <iostream>
#include "XORlinkedlist.h"

int main()
{
XORlinkedList<int> X;

X.addAtBegin(3);
X.addAtBegin(4);
X.addAtBegin(5);

std::cin.get();
return 0;
}

错误是:

error C2784: 'Node *XOR(Node *,Node *)' : could not deduce template argument for 'Node *' from 'int'

最佳答案

只需明确指定一个函数模板参数,例如

temp->XORcode = XOR<T>( top, NULL );

temp->XORcode = XOR<T>( top, nullptr );

关于c++ - 无法从 'Node<T> *' 推断出 'int' 的模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57133222/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com