简体   繁体   English

具有模板的C ++调用函数具有继承性

[英]C++ Calling function that has template which has inheritance

So I have a class that defines a TileGrid: 所以我有一个定义TileGrid的类:

template<typename T>
class TileGrid { ... };

I fill in the template with a class called ImageTile 我用名为ImageTile的类填充模板

class ImageTile { ... };

I have a child class of ImageTile that I have defined that looks like this: 我有一个ImageTile子类, ImageTile已定义如下:

class FFTWImageTile : public ImageTile { ... };

In a separate file I define the following function: 在一个单独的文件中,我定义以下函数:

void writeDataToFile(TileGrid<ImageStitching::ImageTile> * grid, std::string fileName);

(note: both ImageTile and FFTWImageTile are in the ImageStitching namespace) (注意: ImageTileFFTWImageTile都在ImageStitching命名空间中)

Now all of the above compiles just fine, but when I try to use it I get an error. 现在,以上所有代码都可以正常编译,但是当我尝试使用它时,会出现错误。

Here is an example test case that is using it: 这是一个使用它的示例测试用例:

namespace is = ImageStitching;
TileGrid<is::FFTWImageTile> * grid = new TileGrid<is::FFTWImageTile> ( ... );
writeTranslationsToFile(grid, "output.txt");

When compiling the test case I get the following error: 编译测试用例时,出现以下错误:

error: cannot convert 'TileGrid<ImageStitching::FFTWImageTile>*' to 'TileGrid<ImageStitching::ImageTile>*' for argument '1' to 'void writeTranslationsToFile(TileGrid<ImageStitching::ImageTile>*, std::string)'

Anyway I can make this happen in C++?? 无论如何,我可以在C ++中做到这一点? I've looked all over and cant seem to find some help with making a function that has a parameter featuring a template that has child/parent relationships. 我四处张望,似乎无法找到一些帮助,使该函数的参数带有具有子/父关系的模板。

Edit: 编辑:

Everyone's answers have been exceptional and each solve the issue presented. 每个人的答案都非常出色,每个人都能解决所提出的问题。 I think decided to move to C++11 and use an assert for this particular case. 我认为决定移至C ++ 11并针对此特定情况使用断言。 In the future I think I will add a template to the function and ensure to get the data that way. 将来,我想我将向该函数添加一个模板,并确保以这种方式获取数据。 Thank you all for the help! 谢谢大家的帮助! I have marked what I think is the best answer although each have been acceptable. 我已经标出了我认为最好的答案,尽管每个答案都是可以接受的。

You are getting the error because despite that FFTWImageTile is derived from ImageTile , TileGrid<FFTWImageTile> and TileGrid<ImageTile> are absolutely unrelated classes. 之所以会出现错误,是因为尽管FFTWImageTile是从ImageTile派生的, ImageTile TileGrid<FFTWImageTile>TileGrid<ImageTile>绝对是不相关的类。

How to fix this depends on the implementation of the classes which you haven't shown. 如何解决此问题取决于您未显示的类的实现。 Perhaps you can make writeDataToFile() templated: 也许您可以使writeDataToFile()模板化:

template<typename T>
void writeDataToFile(TileGrid<T> * grid, std::string fileName);

You generate two different classes with 您使用生成两个不同的类

TileGrid<ImageStitching::ImageTile>

and

TileGrid<is::FFTWImageTile>

They have no relation other than being generated from the same template. 它们除了从同一模板生成之外没有其他关系。 You would want to derive from the class TileGrid<ImageStitching::ImageTile> to have an actual subclass for type purposes. 您可能希望从类TileGrid<ImageStitching::ImageTile>派生一个具有实际子类的类型用途。

You could templatize the function writeDataToFile , but to enforce type restrictions on template arguments, you should use something like... 您可以对函数writeDataToFile模板化,但是要对模板参数实施类型限制,则应使用类似...

static_assert(is_base_of<T, ImageStitching::ImageTile>>(), "T is not a base of ImageTile");

Options that might work for you. 适合您的选项。

  1. Make writeDataToFile a function template. 使writeDataToFile函数模板。

     template <typename T> void writeDataToFile(TileGrid<T> * grid, std::string fileName); 

    In the implementation of this function, use another function template to write individual elements of the tile. 在此功能的实现中,请使用另一个功能模板来编写图块的各个元素。

     template <typename T> void writeTileElement(T const& tileElement, std::ostream& out); 

    You can create overloaded versions of writeTileElement to take care of special handling of different types of objects. 您可以创建writeTileElement重载版本,以对不同类型的对象进行特殊处理。

  2. Use a TileGrid of ImageTile* instead of ImageFile . 使用TileGridImageTile*代替ImageFile

     void writeDataToFile(TileGrid<ImageStitching::ImageTile*> * grid, std::string fileName); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM