Question

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.

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.

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

For the moment it compiles but when I declare the myMethod like this :

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

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.

Was it helpful?

Solution

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.

OTHER TIPS

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). 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> >. This is because your return type is std::vector<T> and your T is std::vector<std::string>. The code below (representing interface.h) compiles fine on mac os x using clang++ v 3.3.

#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...
    }

};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top