2013-01-31 2 views
0

그래서이 문제가 있습니다. 지금은C++ 템플릿 인터페이스에서 다중 상속

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

없음 문제 :

Basicly은 내가 템플릿 인터페이스를 가지고있다. 그래서이 인터페이스에서 상속받을 클래스를 선언합니다.

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

이제는 cpp 파일의 myMethod 암호를 해독합니다.

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

지금까지 아무런 문제가 없습니다. 두 번째 클래스에서 myMethod 함수를 선언하고 T 유형은 반환 값과 동일하므로 컴파일 오류가 발생합니다. 이 컴파일 순간

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

하지만이 같은 MyMethod라는를 선언 할 때 :

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

을 여기에 나는 표준 : : 벡터 반환 값 FOT 오류 및 방법 인자의 하나를 얻을. 템플릿 충돌이라고 생각하지만 실제로이 문제를 해결하는 방법을 모르겠습니다.

첫 번째 오류 :

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 

두 번째 오류 : 난 여전히 C 코드 ++ 빠르게 배울 수 있지만, 한 번에 한 동안 나는 박히면서 어떤 도움을 필요로하는 방법을 배우고

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

. 내가 잘못하려는 방식입니까? 이 충돌을 피하기 위해 세 번째 typename을 선언해야합니까? (나는 두 개의 typenmaes가 같은 유형이기 때문에 다른 충돌을 줄 것이라고 생각했다.)

내가하는 일은 최선의 방법이 아닐지 모르지만 나는 여전히 배우고있다.

자세한 내용이 필요하면 주저하지 말고 가능한 한 간단하게 예제 코드를 작성해보십시오.

모든 도움말은 매우 복잡해집니다. 감사합니다. .

답변

2

두 번째 수준의 기능 과부하 그 반환 값이 있어야 할 것 같다 : 당신이 사용하는 어떤 컴파일러

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

는, GCC는 템플릿 사양에 오류가 발생했다.

+0

감사합니다. 나는 그렇게 보지 못했습니다. 나는 바보입니다. 벡터로 실습 할 때 나는 정말로 carfull이되어야한다. ^^ – Jp1987

+0

자신이 호의를 베푼다. 클래스 템플릿에서 물건을 typedef'ing하면, 이렇게 못생긴 표정을 피할 수있다. – bstamour

+0

조언 해 주셔서 감사합니다. 나는 기본적으로 테스트를 위해 이와 같은 작업을하고 있으며 어떻게 작동 하는지를 exacly 살펴보기 위해 학습하고 있습니다. – Jp1987

2

나는 템플릿 코드를 .h 파일로 유지하는 것이 가장 좋습니다 (코드는 모든 번역 단위에서 사용할 수 있어야합니다. 따라서 .cpp 파일에 넣으면 컴파일하지만 포함 시키십시오.). 귀하의 코드 (iInterface 대 IInterface 대 Iinterface 등)의 오타 이외에도 두 번째 클래스의 메서드는 std::vector<std::vector<std::string> >을 반환해야합니다. 반환 유형이 std::vector<T>이고 T가 std::vector<std::string>이기 때문입니다. 아래의 코드 (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... 
    } 

}; 
+0

고맙습니다. 문제가있는 모든 잘못된 곳을 찾고있었습니다 ^^ – Jp1987