开发者

how to solve following problem in C++?

开发者 https://www.devze.com 2023-02-15 18:44 出处:网络
I have one template function which will take a pointer type and i have instantiated it before calling.

I have one template function which will take a pointer type and i have instantiated it before calling. i have written function with its dummy implementation as follows:

template<T>fun_name( const T *p )
{
  //written functionality which will give me class name that i will store into string Variable
  e.g. i got output like this string Var = "First_class" or string Var = "Second_class"   

  //Using this class name i will call one function of that class
    if(Var == "Fisrt_class") 
    {   
    First_class::static_function_name(p);
    }
    if(Var == "Second_class")
    { 
    Second_class::static_function_name(p);
    }

}

and in global scope i instantiated this function for two variables as like below:

template<first_class>static_function_name(const First_class *)
template<Second_class>static_function_name(const Second_class开发者_JAVA技巧 *)

above code gives me error that

error: no matching function call in Second_class::static_function_class(const Fisrt_class*)
error: no matching function call in First_class::static_function_class(const Second_class*)

thanks in advance!


I think this :

template<typename T> // template<class T> is equally valid!
void fun_name( const T *p )
{
  T::static_function_name(p);
}

is enough!

Two more errors is fixed in the above code:

  • Mention the keyword typename in template<T> in your code. You can also write template<class T> which is equally valid.
  • Mention the return type of the function template as well.


Your function template "calls" each of the static functions in each class. Even though program flow may never get to one of the calls, the compiler still has to figure out the code for each of them.

So when you instantiate:

template<first_class>fun_name(const first_class*)

the compiler tries to compile the entire function with T = first_class, which means at some point inside the function, it will try to compile the function call:

Second_class::static_function_name(p);

But since variable p is a pointer to first_class, the compiler doesn't find the function.

If you want conditional compilation, try specializing your function instead so the compiler only compiles the function call you intended for each type:

template <T> fun_name (const T* p);
template <> fun_name<first_class>(const first_class* p) {
    first_class::static_function_name(p);
}
template <> fun_name<second_class>(const second_class* p) {
    second_class::static_function_name(p);
}

Alternatively, you can use member functions which seem to be intended for what you are trying to do here. Then you can create objects and call the functions directly:

first_class f;
second_class s;

f.function();
s.function();


try changing to ,

template<typename T>
void fun_name( const T *p )
{
  T::static_function_name(p);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消