简体   繁体   English

从接口的C ++模板多重继承

[英]c++ template multiple inheritance from an interface

So I have this problem. 所以我有这个问题。

Basicly I have a templated interface: 基本上我有一个模板化的接口:

template <typename T, typename U>
class                    Iinterface
{
 public:
 virtual ~Iinterface()
 // A pure methode for the example that gives me the problem
 virtual std::vector<T>        myMethod(T, U) = 0;
};

No problem for now. 现在没问题。 So I declare a class that will inherit from this interface. 因此,我声明了一个将从该接口继承的类。

class                  firstclass : public Iinterface<std::string, int>
{
  // content doesnt matter. The problem is in second class inheritance.
  // here I just put the class so we can see how I inherited in 1st class.
};

Now the myMethod decleration in the cpp file. 现在将myMethod declecle在cpp文件中。

template <>
std::vector<std::string>        firstClass::iInterface<std::string, int>::myMethod(std::string a, int b)
 {
      // methods code
 }

So far I have no problem. 到目前为止,我没有问题。 It is in my second class where I declare the myMethod function and that the type T is the same as the return value that it gives me a compilation error. 在第二个类中,我声明了myMethod函数,并且类型T与返回值相同,这给了我编译错误。

class                           secondClass : public IInterface<std::vector<std::string>, int>
{
  // class content
};

For the moment it compiles but when I declare the myMethod like this : 目前它可以编译,但是当我这样声明myMethod时:

template <>
std::vector<std::string>                     secondClass::Iinterface<std::vector<std::string> a, int n>
{
  // methodes code
}

Here I get an error fot the std::vector return value and the one in the methods argument. 在这里,我得到了一个错误:std :: vector返回值和方法参数中的返回值。 I believe it is a template conflict but I really don't see how to go around this. 我认为这是模板冲突,但我真的不知道如何解决。

First error: 第一个错误:

28 :template-id ‘_out<>’ for ‘std::vector<std::basic_string<char> > Iio<std::vector<std::basic_string<char> >, int>::_out(std::vector<std::basic_string<char> >, int)’ does not match any template declaration

Second error: 第二个错误:

28 note: saw 1 ‘template<>’, need 2 for specializing a member function template

I am still learning how to code in c++ and learn fast but once in a while I get stuck and need some help. 我仍在学习如何使用C ++进行编码并快速学习,但是有时我会陷入困境并需要一些帮助。 Is the way I am trying to do this wrong? 我试图做这件事的方式不对吗? Do I need to declare a third typename to go around this conflict? 我需要声明第三种类型名来解决此冲突吗? (I was thinking it will give just an other conflict because the two typenmaes are of the same type). (我以为这会带来其他冲突,因为两个typenmaes属于同一类型)。

I know what I am trying to do might not be the best way to do this but I am still learning. 我知道我尝试做的事情可能不是最好的方法,但我仍在学习。

I tryed to make example code as simple as possible to explain my problem if you need more details dont hesitate to ask. 我试图使示例代码尽可能简单,以解释我的问题,如果您需要更多详细信息,请不要犹豫。

All help will be very apriciated. 所有帮助将非常宝贵。 Thanks. 谢谢。

It seems that return value for the second class function overload should be: 似乎第二类函数重载的返回值应该是:

std::vector<std::vector<std::string> >

What compiler are you using, gcc gave error at template specifications. 您使用的是什么编译器,gcc在模板规格上给出了错误。

I think as a general rule it is best to keep templated code in .h files (the code has to be available in all translation units so if you put it in .cpp files, you do not compile them but include them). 我认为一般而言,最好将模板代码保存在.h文件中(该代码必须在所有翻译单元中都可用,因此,如果将其放在.cpp文件中,则无需编译,而是将其包括在内)。 Apart from the typos in your code (iInterface vs IInterface vs Iinterface, etc.) your method in the second class should return std::vector<std::vector<std::string> > . 除了代码中的错别字(iInterface,IInterface和Iinterface等)之外,第二类中的方法还应返回std::vector<std::vector<std::string> > This is because your return type is std::vector<T> and your T is std::vector<std::string> . 这是因为您的返回类型为std::vector<T>而您的T为std::vector<std::string> The code below (representing interface.h) compiles fine on mac os x using clang++ v 3.3. 下面的代码(表示interface.h)使用clang ++ v 3.3在mac os x上可以正常编译。

#include <string>
#include <vector>


template <typename T, typename U>
class Iinterface
{
public:
    virtual ~Iinterface();
    // A pure methode for the example that gives me the problem
    virtual std::vector<T> myMethod(T, U) = 0;
};

class firstclass : public Iinterface<std::string, int>
{
    // content doesnt matter. The problem is in second class inheritance.
    // here I just put the class so we can see how I inherited in 1st class.

    std::vector<std::string> myMethod(std::string a, int b)
    {
        // methods code
        // don't forget to return...
    }

};

class secondClass : public Iinterface<std::vector<std::string>, int>
{
    // class content
    std::vector<std::vector<std::string> > myMethod(std::vector<std::string> a, int n)
    {
        // methodes code
        // don't forget to return...
    }

};

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

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