冰柠配绿茶 2022-05-10 11:43 采纳率: 100%
浏览 22
已结题

C++类模板推断失败报错怎么解决?

以下是库文件里的函数和结构,我使用了模板:

#define T template<typename TYPE>

// 单链表节点定义
T struct list_node{
    TYPE data; // 节点数据域
    list_node<TYPE>* next_node; // 节点指针域
};

// 设置节点数据域的值
T void node_set_val(list_node<TYPE>* node, TYPE val){
    node->data = val;
}

// 设置节点指针域的地址
T void node_set_next(list_node<TYPE>* node, list_node<TYPE>* next){
    node->next_node = next;
}

// 单链表定义
T struct linked_list{
    list_node<TYPE> body_pointer; // 链表哨兵节点
    list_node<TYPE>* head_node; // 链表体首节点指针
    list_node<TYPE>* tail_node; // 链表体尾结点指针
    int cnt;
};

// 节点初始化
T list_node<TYPE>* node_init(TYPE data){
    list_node<TYPE>* node = (list_node<TYPE>*)malloc(sizeof(list_node<TYPE>));
    node_set_val(node, data);
    node_set_next(node, NULL);
    return node;
}

// 单链表初始化
T linked_list<TYPE>* list_init(){
    linked_list<TYPE>* list = (linked_list<TYPE>*)malloc(sizeof(linked_list<TYPE>));
    list->body_pointer = *node_init<TYPE>(INT_MAX);
    list->head_node = NULL;
    list->tail_node = NULL;
    list->cnt = 0;
    return list;
}

主函数调用是这样的:

int main(){
    linked_list<int>* list = list_init<int>();
    return 0;
}

但是调用时编译器报错,说无法推断参数类型:

In file included from D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\test_linked_list.cpp:1:
D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\linked_list.h: In instantiation of 'list_node<TYPE>* node_init(TYPE) [with TYPE = int]':
D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\test_linked_list.cpp:6:45:   required from here
D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\linked_list.h:44:18: error: no matching function for call to 'node_set_next(list_node<int>*&, NULL)'
     node_set_next(node, NULL);
     ~~~~~~~~~~~~~^~~~~~~~~~~~
D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\linked_list.h:28:8: note: candidate: 'template<class TYPE> void node_set_next(list_node<TYPE>*, list_node<TYPE>*)'
 T void node_set_next(list_node<TYPE>* node, list_node<TYPE>* next){
        ^~~~~~~~~~~~~
D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\linked_list.h:28:8: note:   template argument deduction/substitution failed:
D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\linked_list.h:44:18: note:   mismatched types 'list_node<TYPE>*' and 'int'
     node_set_next(node, NULL);
     ~~~~~~~~~~~~~^~~~~~~~~~~~
D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\linked_list.h: In instantiation of 'list_node<TYPE>* list_pop(linked_list<TYPE>*, int) [with TYPE = int]':
D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\test_linked_list.cpp:30:52:   required from here
D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\linked_list.h:185:18: error: no matching function for call to 'node_set_next(list_node<int>*&, NULL)'
     node_set_next(node, NULL);
     ~~~~~~~~~~~~~^~~~~~~~~~~~
D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\linked_list.h:28:8: note: candidate: 'template<class TYPE> void node_set_next(list_node<TYPE>*, list_node<TYPE>*)'
 T void node_set_next(list_node<TYPE>* node, list_node<TYPE>* next){
        ^~~~~~~~~~~~~
D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\linked_list.h:28:8: note:   template argument deduction/substitution failed:
D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\linked_list.h:185:18: note:   mismatched types 'list_node<TYPE>*' and 'int'
     node_set_next(node, NULL);
     ~~~~~~~~~~~~~^~~~~~~~~~~~
D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\linked_list.h: In instantiation of 'list_node<TYPE>* list_pop(linked_list<TYPE>*) [with TYPE = int]':
D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\linked_list.h:179:24:   required from 'list_node<TYPE>* list_pop(linked_list<TYPE>*, int) [with TYPE = int]'
D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\test_linked_list.cpp:30:52:   required from here
D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\linked_list.h:165:18: error: no matching function for call to 'node_set_next(list_node<int>*&, NULL)'
     node_set_next(node, NULL);
     ~~~~~~~~~~~~~^~~~~~~~~~~~
D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\linked_list.h:28:8: note: candidate: 'template<class TYPE> void node_set_next(list_node<TYPE>*, list_node<TYPE>*)'
 T void node_set_next(list_node<TYPE>* node, list_node<TYPE>* next){
        ^~~~~~~~~~~~~
D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\linked_list.h:28:8: note:   template argument deduction/substitution failed:
D:\FrozenLemonTee\Arbeit\Programmiersprache\C&C++\src\data_sctructure_in_408\main body\c2\linked_list\linked_list.h:165:18: note:   mismatched types 'list_node<TYPE>*' and 'int'
     node_set_next(node, NULL);
     ~~~~~~~~~~~~~^~~~~~~~~~~~
mingw32-make.exe[3]: *** [CMakeFiles\test_linked_list.dir\build.make:82: CMakeFiles/test_linked_list.dir/src/data_sctructure_in_408/main_body/c2/linked_list/test_linked_list.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:4522: CMakeFiles/test_linked_list.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:4529: CMakeFiles/test_linked_list.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:2112: test_linked_list] Error 2

请问如何解决呢?

  • 写回答

1条回答 默认 最新

报告相同问题?

问题事件

  • 系统已结题 5月18日
  • 已采纳回答 5月10日
  • 创建了问题 5月10日

悬赏问题

  • ¥15 PFENet的预训练权重
  • ¥15 程序哪有错误怎么改?
  • ¥15 交换机和交换机之间的链路带宽以及主机带宽的理解
  • ¥15 ai创想家对战模式代码
  • ¥15 集合A由3个2行4列二维数组构成,从集合A中任意取一个二维数组元素、如果该二维数组元素的对应列位置的上、下两数都是奇数,而且仅有2个列是奇数/奇数,则该数组有意义,并放入集合B中打印输出。
  • ¥15 电信IPV6 无法外网访问吗
  • ¥15 有偿求效果比较好的遥感影像匹配的c++代码
  • ¥15 博主,你好,我下载了你的智能网联汽车辅助驾驶安全信息检测系统,现在不会运行,可以教我吗,
  • ¥15 怎么在excle输入下列公式
  • ¥15 Arduino,利用modbus的RS485协议,进行对外置的温湿度传感器进行数据读取