2013-02-12 2 views
0

선형 대수 패키지에서 치수에 템플릿 기반 템플릿을 제공합니다. 내 프로그램에 대한 테스트 데이터를 생성하는 일부 기능을하고 싶은, 그래서 나뿐만 아니라 크기에 그들을 템플릿에 노력하고, 특정 전문 분야를 가지고 :함수 템플릿 특수화 (int 값으로 템플릿 화 됨)

template <size_t K> 
boost::shared_ptr<std::vector<DistParams<K> > > sampleDistParams() 
{ 
throw "not implemented"; 
} 

template <size_t K> 
boost::shared_ptr<std::vector<DistParams<K> > > sampleDistParams<1>() 
{ 
boost::shared_ptr<std::vector<DistParams<K> > > distParams=(new std::vector<DistParams<K> >()); 
distParams->push_back(DistParams<K>(asMatrix(0.05), asMatrix(0.1))); 
distParams->push_back(DistParams<K>(asMatrix(-0.1), asMatrix(0.2))); 
return distParams; 
} 

template <size_t K> 
boost::shared_ptr<std::vector<DistParams<K> > > sampleDistParams<2>() 
{ 
boost::shared_ptr<std::vector<DistParams<K> > > distParams=(new std::vector<DistParams<K> >()); 
Eigen::Vector2d highMu; 
lowMu << 0.04 << 0.06; 
Eigen::Matrix2d lowSigma; 
highSigma << 0.1 << 0.4 
      << 0.4 << 0.12; 

    Eigen::Vector2d lowMu; 
lowMu << -0.08 << -0.12; 
Eigen::Matrix2d highSigma; 
highSigma << 0.2 << 0.7 
      << 0.7 << 0.24; 

distParams->push_back(DistParams<K>(highMu, lowSigma)); 
distParams->push_back(DistParams<K>(lowMu, highSigma)); 
return distParams; 
} 

그러나,이 물건은 컴파일되지 않습니다. 얻을 수있는 내용 :

/home/ga1009/PhD/cpp/pmi/cpp/test/baumiterationtest.cpp:24:73: error: template-id ‘sampleDistParams<1>’ in declaration of primary template 
/home/ga1009/PhD/cpp/pmi/cpp/test/baumiterationtest.cpp:24:53: error: redefinition of ‘template<unsigned int K> boost::shared_ptr<std::vector<DistParams<K> > > {anonymous}::sampleDistParams()’ 
/home/ga1009/PhD/cpp/pmi/cpp/test/baumiterationtest.cpp:18:53: error: ‘template<unsigned int K> boost::shared_ptr<std::vector<DistParams<K> > > {anonymous}::sampleDistParams()’ previously declared here 
/home/ga1009/PhD/cpp/pmi/cpp/test/baumiterationtest.cpp:33:73: error: template-id ‘sampleDistParams<2>’ in declaration of primary template 
/home/ga1009/PhD/cpp/pmi/cpp/test/baumiterationtest.cpp:33:53: error: redefinition of ‘template<unsigned int K> boost::shared_ptr<std::vector<DistParams<K> > > {anonymous}::sampleDistParams()’ 
/home/ga1009/PhD/cpp/pmi/cpp/test/baumiterationtest.cpp:18:53: error: ‘template<unsigned int K> boost::shared_ptr<std::vector<DistParams<K> > > {anonymous}::sampleDistParams()’ previously declared here 

무엇이 잘못되었으며 어떻게 해결할 수 있습니까?

답변

4

당신은 함수 템플릿을 전문으로하고 싶다면, 당신이 (당신이 부분적으로 함수 템플릿을 전문으로 할 수 있습니다) 템플릿 매개 변수를 재 선언 안 : 특정 경우

template<size_t K> void foo() { } // Primary template 

template<size_t K> void foo<1>() { } // ERROR: Redefinition 
template<> void foo<1>() { } // OK: Specialization 

오른쪽 서명을 템플릿 특성화 없다 :

또한
template<> 
boost::shared_ptr<std::vector<DistParams<1> > > sampleDistParams<1>() 

통지 폐쇄 각괄호 (>) 사이의 C++ (11 개)의 공간이 더 필요하다.

1

잘못된 구문 :

템플릿 특수화은 다음과 같아야합니다

template <> 
boost::shared_ptr<std::vector<DistParams<K> > > sampleDistParams<1>() 
+0

는 아직도'K'가 포함되어 있습니다. – aschepler