模板和无效函数调用C++
Templates and void function calls C++
所以我的错误是,当我调用 void 函数时,我收到一条错误消息,指出没有匹配的函数调用test_string。下一个错误指出候选模板被忽略:无法推断模板参数"T"。 我对模板很陌生,我不确定为什么当我的test_string函数没有错误并且所有模板化函数都在同一个文件中时会出现此错误。
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//typedef string T;
//=======FUNCTION DECLARATION==========
template<typename T>
T* add_entry(T* list, const T& new_entry,
int& size, int& capacity);
template<typename T>
T* get_entry(T* list, const T& new_entry,
int& size, int& capacity);
template<typename T>
T* remove_entry(T* list, const T& delete_me,
int& size, int& capacity);
template<typename T>
T* allocate(int capacity);
template<typename T>
void copy_list(T *dest, T* src, int many_to_copy);
template<typename T>
void release(T* list, int size);
template<typename T>
T* search_entry(T* list, const T& find_me, int size);
template<typename T>
void print_list(T* list, int size);
template<typename T>
void test_string();
int main(){
//no matching function call
test_string();
//the main error
//candidate template ignored
//couldn't infer template 'T'
return 0;
}
//=======FUNCTION DEFINITION==========
template<typename T>
T* add_entry(T* list, const T& new_entry,
int& size, int& capacity){
return get_entry(list,new_entry,size,capacity);
}
template<typename T>
T* get_entry(T* list, const T& new_entry,
int& size, int& capacity){
// T* walker = new T[size];
// walker = list;
list = new T[size];
for(int i = 0;i<size+2;i++){
// *walker = new_entry;
list[i]=new_entry;
size++;
if(size==capacity){
capacity*=2;
}
list++;
}
return list;
}
template<typename T>
T* remove_entry(T* list, const T& delete_me,
int& size, int& capacity){
}
template<typename T>
T* allocate(int capacity){
const bool debug = false;
if(debug) cout<<"allocate: capacity: "<<capacity<<endl;
return new T[capacity];
}
template<typename T>
void copy_list(T *dest, T* src, int many_to_copy){
for(int i = 0;i<many_to_copy;i++){
dest = src;
}
}
template<typename T>
void release(T* list, int size){
for(int i = 0;i<size;i++){
list++;
}
delete list;
}
template<typename T>
T* search_entry(T* list, const T& find_me, int size){
for(int i = 0;i<size;i++){
if(*list==find_me){
return list;
}
list++;
}
}
template<typename T>
void print_list(T* list, int size){
for(int i = 0;i<size;i++){
cout<<*list;
}
// cout<<endl;
}
template<typename T>
void test_string(){
int cap = 3;
int size = 0;
T* list = allocate;
list = add_entry(list,"Erika" , size, cap);
print_list(list,size);
}
正是错误所说的 - 编译器不知道T
应该是什么。你打电话给:
test_string<int>();
test_string<char>();
test_string<string>();
test_string<SomeReallyComplexClassDefinedElsewhere>();
你明白了...
您需要指定T
。这可以做到:
- 显式,如项目符号列表中的示例调用
- 隐式,如果函数签名允许。如果存在类型
T
的参数,则编译器可以从传递给函数的实际参数中扣除T
。
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 类型擦除的std::function与虚拟函数调用的开销