프로그램 목표: 많이 쓰이는 STL template class 중의 하나인 vector를 상속하여
bvector라는 클래스를 만들고 테스트하기.
프로그램 코드:
#include "stdafx.h"
#include <vector>
template<class Type, class Alloc = allocator<Type> >
class bvector: public std::vector<Type, Alloc> { };
using namespace std;
void main() {
vector<int> A(3); //a vector of size 3
bvector<int> B(3); //a bvector of size 3
}
위의 코드를 컴파일하면 bvector<int> B(3); 에서 다음과 같은 좀 긴 에러가
나옵니다.(Visual C에서 컴파일했는데, Borland C는 지워서 컴파일못해봤어요. Borland C에서 하면 에러가 안 나려나?)
error C2664: 'bvector<Type,Alloc>::bvector(const bvector<Type,Alloc> &)' :
매개 변수 1을(를) 'int'에서 'const bvector<Type,Alloc> &'(으)로 변환할 수
없습니다.
with
[
Type=int,
Alloc=std::allocator<int>
]
and
[
Type=int,
Alloc=std::allocator<int>
]
bvector<int> B(3);를 컴파일러가 복사생성자호출로 오해를 하고 있음을 알 수
있습니다.
컴파일러가 왜 이런 오해를 하는지 이해가 안되는데요.
vector<int> A(3);에서는 에러가 안 나오고(당연히 에러 나올리가 없죠),
상속해서 아무것도 안 만진 클래스 bvector를 사용한
bvector<int> B(3); 에서만 에러가 나오니까 이상하거든요.
혹시 Visual C의 버그?, Borland C에서도 컴파일 안되는 거면 프로그래밍 자체가 잘못된 거일 텐데,
위에 코드를 어떻게 고쳐야 되죠?
|