요소와 크기의 반복 가능한 컨테이너를 수용하는 CreateBST 함수를 오버로드하려고합니다. 코드는 다음과 같습니다.
#include <iostream>
#include <vector>
template <typename ElementType>
struct BTreeNode
{
ElementType value;
BTreeNode<ElementType>* left;
BTreeNode<ElementType>* right;
};
template <typename ContainerType, typename ElementType>
BTreeNode<ElementType>* CreateBST(const ContainerType& elements, const size_t num_elements);
int main()
{
std::vector<int> elems{ 67, 34, 82, 12, 45, 78 };
BTreeNode<int>* btree = CreateBST(elems, elems.size());
}
다음 컴파일러 오류가 발생합니다.
error C2672: 'CreateBST': no matching overloaded function found
error C2783: 'BTreeNode<ElementType> *CreateBST(const ContainerType &,const size_t)': could not deduce template argument for 'ElementType'
누군가가이 오류가 발생하는 이유를 설명 할 수 있습니까? 또한 std :: vector 및 요소 유형과 같은 템플릿 템플릿 매개 변수를 사용하는 함수를 어떻게 선언해야합니까?
누군가가이 오류가 발생하는 이유를 설명 할 수 있습니까?
템플릿 매개 변수는 호출 사이트의 인수에서만 추론 할 수 있습니다. 그 정보, 즉 ElementType
실제로 첫 번째 인수의 유형에 존재 하지만 템플릿은 해당 유형을 추출하는 방법을 모릅니다.
std :: vector 및 요소 유형과 같은 템플릿 템플릿 매개 변수를 사용하는 함수를 어떻게 선언해야합니까?
이렇게 :
template <template <typename...> class ContainerType,
typename ElementType>
BTreeNode<ElementType>* CreateBST(const ContainerType<ElementType>& elements,
const size_t num_elements);
첫 번째 템플릿 매개 변수는 템플릿 템플릿 매개 변수입니다. 이를 통해를 전달 하고 유형 vector<int>
의 int
일부를 추론 할 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다