 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Fiber Optic Guest
|
Posted: Thu Jul 28, 2005 3:08 am Post subject: friend function of templated class |
|
|
I wrote a sample app to test using a function to obtain friend-level
access to a templated class. However, I am getting similar errors from
two separate compilers.
If anyone can recommend what function f1() should look like in order
to get it to work I would appreciate it.
Output and code follows.
Chris
$ g++ --version
g++ (GCC) 3.3.1 (cygming special)
$ g++ template.cpp -Wno-non-template-friend
/cygdrive/c/DOCUME~1/<name>/LOCALS~1/Temp/cchYWCO0.o(.text+0x7e):template.
cpp: undefined reference to `f1(Stuff<int, (int)14>&)'
collect2: ld returned 1 exit status
Microsoft Visual Studio .NET 2003
template1 error LNK2019: unresolved external symbol "void __cdecl
f1(class Stuff<int,14> &)" (?f1@@YAXAAV?$Stuff@H$0O@@@@Z) referenced in
function _main
#include <iostream>
using namespace std;
template<class T, int elements> class Stuff;
template<class T, int elements>
void __cdecl f1(Stuff<int, 14> &stuff)
{
cout << "f1() size is " << stuff.size << endl;
}
template
class Stuff
{
friend void f1(Stuff<T, elements> &stuff);
public:
Stuff();
int getSize() { return size; }
private:
int size;
};
template<class T, int elements>
Stuff <T, elements>::Stuff()
{
size = elements;
cout << "In Stuff(" << elements << ")" << endl;
}
int main(int argc, char* argv[])
{
Stuff
cout << "Stuff size is " << stuff.getSize() << endl;
f1(stuff);
return 0;
}
|
|
| Back to top |
|
 |
Ian Guest
|
Posted: Thu Jul 28, 2005 3:39 am Post subject: Re: friend function of templated class |
|
|
Fiber Optic wrote:
| Quote: | I wrote a sample app to test using a function to obtain friend-level
access to a templated class. However, I am getting similar errors from
two separate compilers.
If anyone can recommend what function f1() should look like in order
to get it to work I would appreciate it.
Output and code follows.
Chris
$ g++ --version
g++ (GCC) 3.3.1 (cygming special)
$ g++ template.cpp -Wno-non-template-friend
/cygdrive/c/DOCUME~1/<name>/LOCALS~1/Temp/cchYWCO0.o(.text+0x7e):template.
cpp: undefined reference to `f1(Stuff<int, (int)14>&)'
collect2: ld returned 1 exit status
Microsoft Visual Studio .NET 2003
template1 error LNK2019: unresolved external symbol "void __cdecl
f1(class Stuff<int,14> &)" (?f1@@YAXAAV?$Stuff@H$0O@@@@Z) referenced in
function _main
#include <iostream
using namespace std;
template
template<class T, int elements
void __cdecl f1(Stuff
try |
void f1(Stuff<T, elements> &stuff)
| Quote: | {
cout << "f1() size is " << stuff.size << endl;
}
template
class Stuff
{
friend void f1(Stuff
try |
friend void f1<T, elements>(Stuff &stuff);
Ian
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Jul 28, 2005 3:40 am Post subject: Re: friend function of templated class |
|
|
Fiber Optic wrote:
| Quote: | I wrote a sample app to test using a function to obtain friend-level
access to a templated class. However, I am getting similar errors from
two separate compilers.
If anyone can recommend what function f1() should look like in order
to get it to work I would appreciate it.
[..]
#include <iostream
using namespace std;
template
template<class T, int elements
|
Drop the line above. The 'f1' you're defining here is not a template.
| Quote: | void __cdecl f1(Stuff
{
cout << "f1() size is " << stuff.size << endl;
|
You cannot use 'stuff' here since 'Stuff' hasn't been defined. The body
of this function should be moved below.
| Quote: | }
template
class Stuff
{
friend void f1(Stuff
|
Here you're declaring 'f1' as a non-template function.
| Quote: | public:
Stuff();
int getSize() { return size; }
private:
int size;
};
template<class T, int elements
Stuff
{
size = elements;
cout << "In Stuff(" << elements << ")" << endl;
}
int main(int argc, char* argv[])
{
Stuff
cout << "Stuff size is " << stuff.getSize() << endl;
f1(stuff);
return 0;
}
|
This should compile:
----------------------------------------------------
#include
using namespace std;
template<class T, int elements> class Stuff;
void f1(Stuff<int, 14> &stuff);
template<class T, int elements>
class Stuff
{
friend void f1(Stuff<T, elements> &stuff);
public:
Stuff();
int getSize() { return size; }
private:
int size;
};
template<class T, int elements>
Stuff <T, elements>::Stuff()
{
size = elements;
cout << "In Stuff(" << elements << ")" << endl;
}
void f1(Stuff
{
cout << "f1() size is " << stuff.size << endl;
}
int main(int argc, char* argv[])
{
Stuff
cout << "Stuff size is " << stuff.getSize() << endl;
f1(stuff);
return 0;
}
----------------------------------------------------
If it doesn't, post the error messages.
V
|
|
| Back to top |
|
 |
Fiber Optic Guest
|
Posted: Thu Jul 28, 2005 5:26 am Post subject: Re: friend function of templated class |
|
|
Thanks Victor. It compiles just fine.
-Chris
Victor Bazarov wrote:
| Quote: | Fiber Optic wrote:
I wrote a sample app to test using a function to obtain friend-level
access to a templated class. However, I am getting similar errors from
two separate compilers.
If anyone can recommend what function f1() should look like in order
to get it to work I would appreciate it.
[..]
#include <iostream
using namespace std;
template
template<class T, int elements
Drop the line above. The 'f1' you're defining here is not a template.
void __cdecl f1(Stuff
{
cout << "f1() size is " << stuff.size << endl;
You cannot use 'stuff' here since 'Stuff' hasn't been defined. The body
of this function should be moved below.
}
template
class Stuff
{
friend void f1(Stuff
Here you're declaring 'f1' as a non-template function.
public:
Stuff();
int getSize() { return size; }
private:
int size;
};
template<class T, int elements
Stuff
{
size = elements;
cout << "In Stuff(" << elements << ")" << endl;
}
int main(int argc, char* argv[])
{
Stuff
cout << "Stuff size is " << stuff.getSize() << endl;
f1(stuff);
return 0;
}
This should compile:
----------------------------------------------------
#include
using namespace std;
template
void f1(Stuff<int, 14> &stuff);
template<class T, int elements
class Stuff
{
friend void f1(Stuff
public:
Stuff();
int getSize() { return size; }
private:
int size;
};
template<class T, int elements
Stuff
{
size = elements;
cout << "In Stuff(" << elements << ")" << endl;
}
void f1(Stuff
{
cout << "f1() size is " << stuff.size << endl;
}
int main(int argc, char* argv[])
{
Stuff
cout << "Stuff size is " << stuff.getSize() << endl;
f1(stuff);
return 0;
}
----------------------------------------------------
If it doesn't, post the error messages.
V
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|