template 클래스에서 friend함수를 쓸 때..?

hshthsh의 이미지

안녕하세요.

template클래스에서 friend함수를 정의할 때 inline함수로 정의하지 않으면
구현 부분을 참조하지 못하던데요.

참고로 볼랜드5.5에서 컴파일을 해보았는데요.
gcc에서는 구지 inline으로 하지 않아도 잘 되는 것 같더라고요..
어짜피 해더에 다 넣은 것인데 이유를 모르겠습니다.

혹시 이유를 아시는 분은 간단한 조언이라도 해주셨으면 합니다.

doldori의 이미지

그거 이상하군요. 원래는 돼야 하는데.
볼랜드가 템플릿을 제대로 지원하지 않기 때문일지도 모르겠네요.
간략한 코드를 올리시든지 Comeau로 시험해 보시죠.

http://www.comeaucomputing.com/tryitout/

현재 표준에 가장 근접하다고 평가받는 컴파일러입니다.

hshthsh의 이미지

http://www.comeaucomputing.com/tryitout/
여기서 해보니 잘 되더라고요. 그리고 vc, gcc에서도 잘 되고요..
정말 볼랜드cpp에서 템플릿 부분이 정의가 잘 않되 있는지 모르겠습니다.

볼랜드컴파일에서 아래 소스를 돌려보면 에러가 납니다.
inline으로 하면 에러가 안나고요..
^에러: Unresolved external 'test(Tes<int>&)'

template <class T>
class Tes
{
public:
T m_i;

friend void test(Tes & t);
};

template <class T>
void test(Tes<T> & t)
{
t.m_i=10;
}

int main()
{

Tes<int> t1;
test(t1);
return 0;
}

doldori의 이미지

아, friend 함수 자체가 템플릿 함수로군요. 제 생각에 이 경우에는 오히려
링크 에러가 나야 맞는 것 같습니다. 위처럼 하면 test는 템플릿이 아닌
일반 함수라는 뜻이 됩니다. 그리고 그런 함수는 존재하지 않으므로 링크 에러가
나는 것이죠. 정확히 하려면 다음과 같이 해야 합니다.

template<typename T> class Tes; // 전방 선언
template<typename T> void test(Tes<T>&); // test가 템플릿임을 선언

template <class T>
class Tes
{
public:
    T m_i;

    friend void test<T>(Tes<T>& t);
//  friend void test<>(Tes& t); 로 해도 됨
}; 

template <class T> 
void test(Tes<T>& t) 
{ 
    t.m_i=10; 
} 

int main() 
{
    Tes<int> t1; 
    test(t1); 
    return 0; 
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.