English 中文(简体)
C++虚拟模板类别遗留问题
原标题:Inheriting from a virtual template class in C++

• 如何使用虚拟模板类别,载于该代码:

// test.h
class Base {
 public:
  virtual std::string Foo() = 0;
  virtual std::string Bar() = 0;
};

template <typename T>
class Derived : public Base {
 public:
  Derived(const T& data) : data_(data) { }

  virtual std::string Foo();
  virtual std::string Bar();

  T data() {
    return data_;
  }

 private:
  T data_;
};


typedef Derived<std::string> DStr;
typedef Derived<int> DInt;

// test.cpp
template<typename T>
std::string Derived<T>::Foo() { ... }
template<typename T>
std::string Derived<T>::Bar() { ... }

当我试图使用DStr或DInt时,联系人抱怨说,没有解决的外在问题,即Derived<std:string >:Foo()Derived<std:string >:Bar(;and the Derived<int>

我是否对我的法典有过错?

EDIT: Thanks all. It s pretty clear now.

最佳回答

You need to define template<typename T> std::string Derived<T>::Foo() { ... } and template<typename T> std::string Derived<T>::Bar() { ... } in the header file. When the compiler is compiling test.cpp it doesn t know all the possible values of T that you might use in other parts of the program.

我认为,有些汇编者在汇编和连接有关步骤之间有关系,这些步骤注意到关于失踪模板即时的提及,并在公布时从申请档案中立即进行。 但我不知道它们是什么,而且其功能极为罕见。

如果你在标题档案中界定这些内容,大多数汇编者将把它们作为软弱的象征,纳入他们重新参考的每个汇编单位。 联系人除对弱小的象征作一个定义外,将全部删除。 这就需要额外的汇编时间。

另一种情况是,有条不紊地明确发布模板,迫使汇编者在其中公布定义。 但是,这要求你们了解所有价值观(<代码>)。 T可能已经存在,你将不可避免地会错失一些。

问题回答

你们必须确保成员的职能在一定地方对所有必要的类别进行即时处理。

通常通过在申报的头号档案中确定模板功能来完成这项工作,以便任何职能的使用都能够立即进行。

作为一种替代办法,在你确定来源档案时,你可以采用明确的即时方式,但这确实要求你事先知道您的模板将随时随便的所有类型。

这确实与衍生物有关。 这只是一个带有模板的一般规则:与大多数汇编者(任何编目,但书目)一样,你必须充分实施一个模板,在每一个翻译单位中看到这一模板——通常在首页上。

即便是同科摩罗联盟一样,你也必须使用<条码>出口<>/代码>的关键词使事情正确。 自2006年以来 它们是执行<条码>出口<>/代码>的唯一办法,其机会相当好,但你却不小心。





相关问题
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?

热门标签