표준 컨테이너의 value_type은 어떻게 사용합니까? 나는이 VALUE_TYPE가 무엇인지 ::라고 생각템플릿 매개 변수의 value_type 사용
prog.cpp:10: error: ‘T::value_type’ is not a type
prog.cpp:14: error: type ‘T’ is not derived from type ‘TSContainer<T>’
prog.cpp:14: error: expected ‘;’ before ‘pop’
prog.cpp:19: error: expected `;' before ‘}’ token
prog.cpp: In function ‘int main()’:
prog.cpp:25: error: ‘class TSContainer<std::vector<int, std::allocator<int> > >’ has no member named ‘pop’
prog.cpp:25: warning: unused variable ‘v’
:
나는 그렇게처럼 사용하려고 :
#include <vector>
using namespace std;
template <typename T>
class TSContainer {
private:
T container;
public:
void push(T::value_type& item)
{
container.push_back(item);
}
T::value_type pop()
{
T::value_type item = container.pop_front();
return item;
}
};
int main()
{
int i = 1;
TSContainer<vector<int> > tsc;
tsc.push(i);
int v = tsc.pop();
}
하지만이 결과?
가능한 복제본 어디에서 왜 "템플릿"및 "typename"키워드를 넣어야합니까?] (http://stackoverflow.com/questions/610245/where-and-why-do-i-have -to-put-the-template-and-typename-keywords) –
재사용 가능한 라이브러리 코드를 작성할 때 (또는 심지어 언제든지)'using namespace std; '를 사용하지 않는 것이 좋습니다. 올바른 네임 스페이스를 입력하십시오. –
@KerrekSB : 예였습니다. –