By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,925 Members | 1,129 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,925 IT Pros & Developers. It's quick & easy.

friend function of templated class

P: n/a
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 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;
}

int main(int argc, char* argv[])
{
Stuff<int, 14> stuff;

cout << "Stuff size is " << stuff.getSize() << endl;

f1(stuff);

return 0;
}


Jul 28 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Ian
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.

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) try
void f1(Stuff<T, elements> &stuff) {
cout << "f1() size is " << stuff.size << endl;
}

template<class T, int elements>
class Stuff
{
friend void f1(Stuff<T, elements> &stuff);

try
friend void f1<T, elements>(Stuff &stuff);

Ian
Jul 28 '05 #2

P: n/a
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<class T, int elements> class Stuff;
template<class T, int elements>
Drop the line above. The 'f1' you're defining here is not a template.
void __cdecl f1(Stuff<int, 14> &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 T, int elements>
class Stuff
{
friend void f1(Stuff<T, elements> &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 <T, elements>::Stuff()
{
size = elements;
cout << "In Stuff(" << elements << ")" << endl;
}

int main(int argc, char* argv[])
{
Stuff<int, 14> stuff;

cout << "Stuff size is " << stuff.getSize() << endl;

f1(stuff);

return 0;
}


This should compile:
----------------------------------------------------
#include <iostream>
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<int, 14> &stuff)
{
cout << "f1() size is " << stuff.size << endl;
}

int main(int argc, char* argv[])
{
Stuff<int, 14> stuff;
cout << "Stuff size is " << stuff.getSize() << endl;
f1(stuff);
return 0;
}
----------------------------------------------------
If it doesn't, post the error messages.

V
Jul 28 '05 #3

P: n/a
Thanks Victor. It compiles just fine.
-Chris

Victor Bazarov wrote:
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<class T, int elements> class Stuff;
template<class T, int elements>

Drop the line above. The 'f1' you're defining here is not a template.

void __cdecl f1(Stuff<int, 14> &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 T, int elements>
class Stuff
{
friend void f1(Stuff<T, elements> &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 <T, elements>::Stuff()
{
size = elements;
cout << "In Stuff(" << elements << ")" << endl;
}

int main(int argc, char* argv[])
{
Stuff<int, 14> stuff;

cout << "Stuff size is " << stuff.getSize() << endl;

f1(stuff);

return 0;
}

This should compile:
----------------------------------------------------
#include <iostream>
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<int, 14> &stuff)
{
cout << "f1() size is " << stuff.size << endl;
}

int main(int argc, char* argv[])
{
Stuff<int, 14> stuff;
cout << "Stuff size is " << stuff.getSize() << endl;
f1(stuff);
return 0;
}
----------------------------------------------------
If it doesn't, post the error messages.

V

Jul 28 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.