473,695 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template Friend Function, GCC and C++ FAQ Lite

I posted a few days ago about the same problem but was not very clear. So
here is my second take at it.

Basically with GCC 3.3.2, I can't compile the example [34.15] from the
C++ FAQ Lite available online at
http://www.parashift.com/c++-faq-lit...html#faq-34.15

Below are the two files that I compile with
g++ foo.cpp -o foo
or
g++ -DMAKE_GCC_HAPPY foo.cpp -o foo

Gcc is happy when I use -DMAKE_GCC_HAPPY but if I don't it says:

$ g++ foo.cpp -o foo
In file included from foo.cpp:1:
foo.h:17: warning: friend declaration `Foo<T> operator+(const Foo<T>&, const
Foo<T>&)' declares a non-template function
foo.h:17: warning: (if this is not what you intended, make sure the function
template has already been declared and add <> after the function name here)
-Wno-non-template-friend disables this warning
foo.h:18: warning: friend declaration `std::ostream& operator<<(std: :ostream&,
const Foo<T>&)' declares a non-template function
/tmp/ccYOJ2nR.o(.tex t+0x5c): In function `main':
: undefined reference to `operator+(Foo< int> const&, Foo<int> const&)'
/tmp/ccYOJ2nR.o(.tex t+0x70): In function `main':
: undefined reference to `operator<<(std ::basic_ostream <char, std::char_trait s<char> >&, Foo<int> const&)'
collect2: ld returned 1 exit status

My understanding is that defining the friend functions before the class is
not enough to make obvious to the compiler that they are template
functions.

Any ideas?

Thanks,
Christophe

---- foo.cpp ----
#include "foo.h"
#include <string>
#include <iostream>

using namespace std;

int main()
{
Foo<int> lhs(1);
Foo<int> rhs(2);
Foo<int> result = lhs + rhs;
cout << result << endl;
}
---- end of foo.cpp ----

---- foo.h ----
#include <iostream>

#ifdef MAKE_GCC_HAPPY
#define __TFS__ <>
#else
#define __TFS__
#endif

template<typena me T> class Foo; // pre-declare the template class itself
template<typena me T> Foo<T> operator+ (const Foo<T>& lhs, const Foo<T>& rhs);
template<typena me T> std::ostream& operator<< (std::ostream& o, const Foo<T>& x);

template<typena me T>
class Foo {
public:
Foo(const T& value = T());
friend Foo<T> operator+ __TFS__ (const Foo<T>& lhs, const Foo<T>& rhs);
friend std::ostream& operator<< __TFS__ (std::ostream& o, const Foo<T>& x);
private:
T value_;
};

template<typena me T>
Foo<T>::Foo(con st T& value) : value_(value)
{ }

template<typena me T>
Foo<T> operator+ (const Foo<T>& lhs, const Foo<T>& rhs)
{
return Foo<T>(lhs.valu e_ + rhs.value_);
}

template<typena me T>
std::ostream& operator<< (std::ostream& o, const Foo<T>& x)
{
return o << x.value_;
}
---- end of foo.h ----
Jul 19 '05 #1
2 10157
Christophe Barbe wrote:
I posted a few days ago about the same problem but was not very clear.
So here is my second take at it.

Basically with GCC 3.3.2, I can't compile the example [34.15] from the
C++ FAQ Lite available online at
http://www.parashift.com/c++-faq-lite containers-and-templates.html# faq-34.15
Below are the two files that I compile with
g++ foo.cpp -o foo
or
g++ -DMAKE_GCC_HAPPY foo.cpp -o foo

Gcc is happy when I use -DMAKE_GCC_HAPPY but if I don't it says:

$ g++ foo.cpp -o foo
In file included from foo.cpp:1:
foo.h:17: warning: friend declaration `Foo<T> operator+(const Foo<T>&,
const
Foo<T>&)' declares a non-template function
foo.h:17: warning: (if this is not what you intended, make sure the
function
template has already been declared and add <> after the function
name here) -Wno-non-template-friend disables this warning
foo.h:18: warning: friend declaration `std::ostream&
operator<<(std: :ostream&,
const Foo<T>&)' declares a non-template function
/tmp/ccYOJ2nR.o(.tex t+0x5c): In function `main':
: undefined reference to `operator+(Foo< int> const&, Foo<int> const&)'
/tmp/ccYOJ2nR.o(.tex t+0x70): In function `main':
: undefined reference to `operator<<(std ::basic_ostream <char,
: std::char_trait s<char> >&, Foo<int> const&)'
collect2: ld returned 1 exit status

My understanding is that defining the friend functions before the
class is not enough to make obvious to the compiler that they are
template functions.

Any ideas?


gcc is right. In chapter C.13.2 of TC++PL3, Stroustrup writes an
example:
=============== ========== begin quote =============== =============== ==

template<class T> class Matrix;

template<class T> class Vector {
T v[4];
public:
friend Vector operator*<>(con st Matrix<T>&, const Vector&);
// ...
};

template<class T> class Matrix {
Vector<T> v[4];
public:
friend Vector<T> operator*<>(con st Matrix&, const Vector<T>&);
// ...
};

Te <> after the name of the friend function is needed to make clear that
the friend is a template function. Without the <>, a non-template
function would be assumed.

=============== ========== end quote =============== =============== ====
Jul 19 '05 #2
On Mon, 20 Oct 2003 23:52:08 +0200, Rolf Magnus wrote:
gcc is right. In chapter C.13.2 of TC++PL3, Stroustrup writes an
example:


Thanks.
So VC++ is wrong (which is not really a surprise) and the faq has to be
updated. For the former I can do nothing, I will contact the author of the
C++ FAQ lite.

Thanks again,
Christophe
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1596
by: Jason Heyes | last post by:
The following program does not compile. Apparantly "t" is inaccessible. #include <iostream> using namespace std; template <class T> class Foo { T t; public: Foo(T t_) : t(t_) { }
5
2714
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: //**************************************************** // testClass.h //**************************************************** #ifndef TESTCLASS_H
7
2379
by: Thomas Matthews | last post by:
Hi, I am converting my table and record classes into templates. My issue is the syntax of declaring a friend class within the template. I have searched the C++ FAQ Lite (web), the C++ newsgroups, "Thinking In C++" to no avail. Background ----------
9
1825
by: eiji | last post by:
Hi folks, I have a linker - problem using a Matrix-template. Maybe someone can help me with that. Q: "using femath::Matrix" or "using femath::Matrix<double>"; Consider this:
3
1981
by: Patricia | last post by:
Hi all, What is wrong with my following codes? //SortedListOfEvents.hh .... line 45: template < class Element > class SortedListOfEvents { .... line 50: template <class Element> friend ostream& operator << (ostream&, const SortedListOfEvents < Element > &);
1
2106
by: giganut | last post by:
Hi, I'm using MS VS.Net2003, and I'm getting a linker error in a project for a templated friend function Specifically: template <class Tclass Vec3 {...
3
3755
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
4
1906
by: StephQ | last post by:
According to: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.4 , if my understanding is correct, in template<typename T> class Foo { friend void func (const Foo<T>& foo); }; template<typename T>
21
4734
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination with mingw because this code (or at least code that gets the same result) works as expected in visualc++. I know that this is probably not the right behavior for a compiler but it's the kind of behavior I'm searching for so I was hoping there was a way to do the same thing in gcc. As you know...
0
8586
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9004
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4351
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4592
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1986
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.