내 프로그램에 대해 빠른 질문이 있습니다.이 템플릿 함수를 int
이 아닌 Set
으로 어떻게 호출 할 수 있습니까? 나는이 여기 C++ 템플릿 클래스로 템플릿 함수를 호출하려면 어떻게해야합니까?
#include <iostream>
#include <vector>
using namespace std;
template<typename T>
class Set
{
public:
class Iterator;
void add(T v);
void remove(T v);
Iterator begin();
Iterator end();
private:
vector<T> data;
};
설정
라고 여기 클래스 내 CPP의 : 그래서 다른 함수addstuff
, 주요 통화
template <class T>
Set<T> addstuff()
{
Set<T> a;
a.add(1);
a.add(2);
a.add(3);
a.add("a string");
return a;
}
void main()
{
addstuff<Set>(); //<< Error here. If I use addstuff<int>(), it would run but
//I can't add string to it. I am required to be able to add
//different data types to this vector
}
"차이 데이터 유형을 추가 할 수 있어야합니다 ..."- 'boost :: any'을 시도하거나 필수 기본 클래스가있는 다형성 유형으로 제한하십시오. – Niall
타사 라이브러리를 사용할 수 없습니다 ... 불행히도 학교 과제입니다. –
'boost :: any'는 자신을 구현하기가 너무 어렵지 않습니다. 그렇지 않으면 당신이 지원해야 할 유형에 대해 노조를 조사하십시오. – Niall