www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - template friend member

↑ ↓ ← bw <bw_member pathlink.com> writes:
i'm having trouble figuring out how to use a member function in a header
provided like this...

template <class T>
class foo
{
public:
friend int friendfunc(foo<char> &);
// etc...

i need to define this function in a separate file, and i'm having a problem with
the linker finding this function.  anybody know offhand the way to implement and
link this?

for instance i have:
foo.h       // header
foo.cpp     // implementation
friend.cpp  // friendfunc code
main.cpp    // the app

i've #included the foo.cpp and everything about this template works just fine
except this one dang function... everything i do i get Symbol Undefined error
from the linker.

i hate to give up, any ideas?

thanks,
bw
Oct 22 2002
↑ ↓ "Walter" <walter digitalmars.com> writes:
In foo.cpp, you'll need a reference to the template in order to cause its
instantiation.

"bw" <bw_member pathlink.com> wrote in message
news:ap53g6$eti$1 digitaldaemon.com...
 i'm having trouble figuring out how to use a member function in a header
 provided like this...

 template <class T>
 class foo
 {
 public:
 friend int friendfunc(foo<char> &);
 // etc...

 i need to define this function in a separate file, and i'm having a

 the linker finding this function.  anybody know offhand the way to

 link this?

 for instance i have:
 foo.h       // header
 foo.cpp     // implementation
 friend.cpp  // friendfunc code
 main.cpp    // the app

 i've #included the foo.cpp and everything about this template works just

 except this one dang function... everything i do i get Symbol Undefined

 from the linker.

 i hate to give up, any ideas?

 thanks,
 bw

Oct 23 2002
↑ ↓ bw <bw_member pathlink.com> writes:
In article <ap6hf1$2d41$2 digitaldaemon.com>, Walter says...
In foo.cpp, you'll need a reference to the template in order to cause its
instantiation.

here's a small example, it's no big deal, just curious... this is part of a project from a friend's cs255 class. if ya have time to help thanks walter, brian //foo.h #ifndef FOO_H #define FOO_H template<class T> class foo { public: foo(); friend int ff(foo<char> &); private: char d[1]; }; #endif // foo.cpp #include "foo.h" template<class T> foo<T>::foo() { d[0]='$'; } // ff.cpp #include "foo.h" int ff(foo<char> &f) { int t=f.d[0]; return t; } // main.cpp #include <iostream.h> #include "foo.cpp" #include "ff.cpp" int main() { foo<char> X; int x=ff(X); cout << x; return 0; } C:\cpp\tpl\f>sc main link main,,,user32+kernel32/noi; OPTLINK (R) for Win32 Release 7.50B1 Copyright (C) Digital Mars 1989 - 2001 All Rights Reserved main.obj(main) Error 42: Symbol Undefined ?ff YAHAAV?$foo D Z (int cdecl ff(foo<char > &)) --- errorlevel 1
Oct 23 2002
↑ ↓ "Walter" <walter digitalmars.com> writes:
Put in foo.cpp the following:

    foo<char> x;

The trick is the compiler, in ff.cpp and main.cpp, does not have the
template definition so cannot instantiate the template. In foo.cpp, the
compiler doesn't know about the uses of foo in ff.cpp and main.cpp, and in
foo.cpp the compiler doesn't know what types to instantiate it with.

"bw" <bw_member pathlink.com> wrote in message
news:ap6mgp$2its$1 digitaldaemon.com...
 In article <ap6hf1$2d41$2 digitaldaemon.com>, Walter says...
In foo.cpp, you'll need a reference to the template in order to cause its
instantiation.

here's a small example, it's no big deal, just curious... this is part of

 project from a friend's cs255 class.
 if ya have time to help thanks walter,
 brian

 //foo.h
 #ifndef FOO_H
 #define FOO_H
 template<class T>
 class foo
 {
 public:
 foo();
 friend int ff(foo<char> &);
 private:
 char d[1];
 };
 #endif

 // foo.cpp
 #include "foo.h"
 template<class T>
 foo<T>::foo()
 {
 d[0]='$';
 }

 // ff.cpp
 #include "foo.h"
 int ff(foo<char> &f)
 {
 int t=f.d[0];
 return t;
 }

 // main.cpp
 #include <iostream.h>
 #include "foo.cpp"
 #include "ff.cpp"

 int main()
 {
 foo<char> X;
 int x=ff(X);
 cout << x;
 return 0;
 }

 C:\cpp\tpl\f>sc main
 link main,,,user32+kernel32/noi;
 OPTLINK (R) for Win32  Release 7.50B1
 Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved

 main.obj(main)
 Error 42: Symbol Undefined ?ff  YAHAAV?$foo D   Z (int cdecl ff(foo<char >

 --- errorlevel 1

Oct 23 2002
↑ ↓ bw <bw_member pathlink.com> writes:
In article <ap6q8k$2o0l$1 digitaldaemon.com>, Walter says...
Put in foo.cpp the following:

    foo<char> x;

that does it thanks, anybody called you a genius lately? L8r, bw
Oct 23 2002
↑ ↓ "Matthew Wilson" <dmd synesis.com.au> writes:
The eponymous term of ultimate respect is "compiler-walter"

:)

"bw" <bw_member pathlink.com> wrote in message
news:ap6qp3$2on8$1 digitaldaemon.com...
 In article <ap6q8k$2o0l$1 digitaldaemon.com>, Walter says...
Put in foo.cpp the following:

    foo<char> x;

that does it thanks, anybody called you a genius lately? L8r, bw

Oct 23 2002
↑ ↓ → Jan Knepper <jan smartsoft.us> writes:
Walter is not a person... He is a team...



Matthew Wilson wrote:

 The eponymous term of ultimate respect is "compiler-walter"

 :)

 "bw" <bw_member pathlink.com> wrote in message
 news:ap6qp3$2on8$1 digitaldaemon.com...
 In article <ap6q8k$2o0l$1 digitaldaemon.com>, Walter says...
Put in foo.cpp the following:

    foo<char> x;

that does it thanks, anybody called you a genius lately? L8r, bw


Oct 23 2002