English 中文(简体)
如何在C++中解决下列问题?
原标题:how to solve following problem in C++?
  • 时间:2011-03-10 06:06:00
  •  标签:
  • c++

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

}

在全球范围,对以下两种变数即时履行这一职能:

template<first_class>static_function_name(const First_class *)
template<Second_class>static_function_name(const Second_class *)

以上法典给我留下的错误是:

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*)

页: 1

最佳回答

我认为:

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

够了。

上述法典中还有两处错误:

  • 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.
问题回答

您的职能模板“呼吁”每一类固定职能。 尽管方案流动可能永远不会达到其中一项要求,但汇编者仍必须列出每项呼吁的代码。

因此,当你当面:

template<first_class>fun_name(const first_class*)

汇编者试图将entire<>/em>功能与T = 头等功能汇编成册,这意味着在职能的某些时间,它将努力汇编以下功能:

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

尝试改为:

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




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?