模板和虚函数调用C++
所以我的错误是,当我调用一个void函数时,我得到一条错误消息,说没有匹配的函数调用来测试字符串。下一个错误表示已忽略候选模板:无法推断模板参数“t”。 我对模板非常陌生,当我的test_string函数没有错误并且所有模板函数都在同一个文件中时,我不知道为什么会出现这个错误模板和虚函数调用C++,c++,C++,所以我的错误是,当我调用一个void函数时,我得到一条错误消息,说没有匹配的函数调用来测试字符串。下一个错误表示已忽略候选模板:无法推断模板参数“t”。 我对模板非常陌生,当我的test_string函数没有错误并且所有模板函数都在同一个文件中时,我不知道为什么会出现这个错误 #include <iostream> #include <iomanip> #include <string> using namespace std
#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应该是什么。你在打电话吗 测试字符串; 测试字符串; 测试字符串; 测试字符串; 你明白了 您需要指定T。这可以通过以下方式完成: 显式地,如项目符号列表中的示例调用 如果函数签名允许,则隐式地。如果存在类型为T的参数,则编译器可以从传递给函数的实际参数中扣除T。
test_string依赖于类型T。由于它不带参数,如果您不编写类似test_string的东西,编译器如何知道您不想成为什么?您可以删除95%的代码,并且仍然显示问题。