Templates and void function calls C++

2007 views c++
0

So my error is that when i call a void function I get an error message that says no matching function call to test_string. The next error says candidate template ignored: couldn't infer template argument 'T'. I am pretty new to templates and I am not sure why I am getting this error when my test_string function takes in no error and all templated functions are in the same file.

    #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);

}

answered question

test_string depends on a type T. Since it takes no arguments, how is the compiler supposed to know what you want T to be, if you don't write something like test_string<void>()?

1 Answer

0

Exactly what the error says - the compiler does not know what T is supposed to be. Are you calling:

  • test_string<int>();
  • test_string<char>();
  • test_string<string>();
  • test_string<SomeReallyComplexClassDefinedElsewhere>();

You get the idea...

You need to specify the T. This can be done:

  • explicitly, like in example calls in the bullet list
  • implicitly, if the function signature permits. If there is a parameter of type T then the compiler can deduct T from the actual argument you passed to the function.

posted this

Have an answer?

JD

Please login first before posting an answer.