2014-04-03 3 views
1

는 Y :다른 템플릿에서도 사용할 수 있습니까?

나는 클래스가 (기술적으로) 아무 관계가 없기 때문에 그것을 의심하는거야,하지만 난 다른 템플릿 구현의 친구와 함께 클래스를 만들 수있는 방법은 무엇입니까?

는 X : 나는 교육적인 이유로 내 자신의 고정 소수점 클래스를 만들기로 장난 해요

. 유일한 이유 누구도 다른 고정 점에서 고정 된 지점을 만들 때 될 기본 고정 소수점 값을 얻을 필요가 whould 예를 들어

그래서 기본적으로
template <typename I, size_t R> 
class FixedPoint 
{ 
public: 
    template <typename J, size_t Rj> 
    FixedPoint(const FixedPoint<J,Rj>& ref) 
    { 
     ... 
    std::ptrdiff_t radix_diff = R-Rj; 
    val_ = ref.val_ >> radix_diff;//there is more to this but you get the idea 
    } 
private: 
    I val_; 
} 

, 정말 다음과 같은 사실을하고 싶습니다 :

template <typename J,typename Rj> 
friend FixedPoint<J,Rj>::FixedPoint(const FixedPoint<I,R>& ref); 

분명히 불가능합니다. val_ public에 getter 함수를 만드는 것 이외의 다른 방법이 있습니까?

편집 :

/home/user/source/testdir/main.cpp: In instantiation of 'class FixedPoint<signed char, 4ul>': 
/home/user/source/testdir/main.cpp:115:24: required from here 
/home/user/source/testdir/main.cpp:64:10: internal compiler error: unexpected expression 'Rj' of kind template_parm_index 
    friend FixedPoint<J,Rj>::FixedPoint(const FixedPoint<I,R>& ref); 
     ^
Please submit a full bug report, 
with preprocessed source if appropriate. 
See <http://bugzilla.redhat.com/bugzilla> for instructions. 
Preprocessed source stored into /tmp/ccWaztkQ.out file, please attach this to your bugreport. 
make[2]: *** [CMakeFiles/TestProgram.dir/main.cpp.o] Error 1 
make[1]: *** [CMakeFiles/TestProgram.dir/all] Error 2 
make: *** [all] Error 2 
15:17:06: The process "/usr/bin/make" exited with code 2. 
Error while building/deploying project TestProgram (kit: Desktop) 
When executing step 'Make' 
+0

http://stackoverflow.com/a/3292852/596167 –

+0

입니다 어리석은 교육 프로젝트,하지만 난 내 우정 엄격한 습관입니다 : 나는 단지 생성자에게 그것을주고 싶습니다 : - P – IdeaHat

+0

Btw 당신이 진정으로되고 싶은 코드는 '템플릿 '대신에이 방법을 사용할 수 없습니다. –

답변

0

연타 3.5 : 내가 g에 ++ i는 "내부 컴파일러 오류"를 얻을, 이것이 가능하지 않은 경우 실제로는 확실하지 않다 것을 추가하는거야

내부 오류를주지는 않지만 템플리트 정의 내에서 템플리트를 인스턴스화 할 수 없습니다 (무한 루프 일 가능성이 있음). 그래서, 대답은 ...

template<typename,typename> friend class FixedPoint; 

가 서로 템플릿 친구의 모든 인스턴스를 만들려면 내 충분한을 workes :(

+1

'friend' 키워드를 잊어 버렸습니다. – Constructor

+0

죄송합니다, 편집 ... – Massa

+0

나는 대답을 하겠지만, 나는 슬픈 얼굴에 완전히 동의합니다. . – IdeaHat

관련 문제