473,748 Members | 11,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template Friend

Given this code:
#include <iostream>
#include <string>

class timestamp
{
public:
timestamp() {
std::cout << "--> default constructor" << std::endl;
}

template <class charT, class traits>
friend
std::basic_ostr eam<charT, traits>&
operator<< (std::basic_ost ream<charT, traits>& strm, const timestamp& ts);

private:
// implementation not relevant
};

template<class charT, class traits>
std::basic_ostr eam<charT, traits>&
operator<<(std: :basic_ostream< charT, traits>& strm, const timestamp& ts)
{
using namespace std;

strm << "--> operator<<()";

return strm;
}

int main()
{
timestamp p;
std::cout << p << std::endl; // line 36
return 0;
}

-------------------------------------------

VC 7.1 (13.10.3077) complains with

bug.cpp(36) : error C2563: mismatch in formal parameter list
bug.cpp(36) : error C2568: '<<' : unable to resolve function overload

Borland C++ 5.6.4 and G++ 3.2 compile without errors.

Who is wrong and who is right?

Changing to

friend
std::ostream&
operator<< (std::ostream& strm, const timestamp& ts);

and the compiler stops complaining.

Josue Gomes
josuegomes at yahoo dot com
Jul 22 '05 #1
1 2225
pa************* *@yahoo.com wrote in news:49a09e7e.0 406290413.3a5c0 18
@posting.google .com in comp.lang.c++:
template<class charT, class traits>
std::basic_ostr eam<charT, traits>&
operator<<(std: :basic_ostream< charT, traits>& strm,
const timestamp& ts)
{
using namespace std;

strm << "--> operator<<()";

return strm;
}

int main()
{
timestamp p;
std::cout << p << std::endl; // line 36
return 0;
}

-------------------------------------------

VC 7.1 (13.10.3077) complains with

bug.cpp(36) : error C2563: mismatch in formal parameter list
bug.cpp(36) : error C2568: '<<' : unable to resolve function overload

Borland C++ 5.6.4 and G++ 3.2 compile without errors.

Who is wrong and who is right?
VC7.1 is wrong you've found a bug.

Changing to

friend
std::ostream&
operator<< (std::ostream& strm, const timestamp& ts);

and the compiler stops complaining.


Yes, but this isn't the same function as your template function
above, simply removing the friend declaration also works-around
the bug.

Workaround: define the friend inline in the class, if its implementation
is unsutible for inlining have it forward to a non-inline template member
instead.
#include <iostream>
#include <ostream>

class timestamp
{
template < typename charT, typename traits>
friend std::basic_ostr eam<charT, traits>& operator<< (
std::basic_ostr eam<charT, traits>& strm, timestamp const& ts
)
{
return ts.m_print( strm );
}

private:

template < typename charT, typename traits>
std::basic_ostr eam<charT, traits>&
m_print( std::basic_ostr eam<charT, traits>& ) const;

};

template < typename charT, typename traits>
std::basic_ostr eam<charT, traits>&
timestamp::m_pr int( std::basic_ostr eam<charT, traits>& strm) const
{
strm << "--> operator<<()";

return strm;
}

int main()
{
timestamp p;
std::cout << p << std::endl;
}

Unfortunatly this doesn't work for Borland C++ 5.6.4, so your
left with making m_print() public and just delaring/defining
operator << () at namespace scope and forwarding to m_print.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

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

Similar topics

2
12536
by: Christophe Barbe | last post by:
I am not clear about friend functions of a template class. GCC (3.3.2) wants me to add <> after the friend function names in the class declaration and VisualC++ doesn't like that. template <class T> class test{ test(void); ~test(void); friend bool operator== <> (const test<T> &p1, const test<T> &p2); }
2
10160
by: Christophe Barbe | last post by:
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 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
1
3342
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me to turn a previously made String class that deals with char's into a templated String class that uses the template parameter C instead of char. I thought it would be fairly simple to do this exercise, but I encoutered many errors for my...
1
4447
by: Dmitry D | last post by:
Hi all, I'm having problems with declaring a template friend function. It seems like I've done everything as explained in C++ FAQ, but still, I'm getting the linker error (unresolved external 'aFunc(A<char> const&)' ) in the following sample program. Tried it on GCC and Borland's compiler. Can somebody please show me the correct way to fix this? Thanks, Dmitry
5
2720
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
6
3331
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for my particular application. One of the advantages is that the _compiler_ can force inner matrix dimensions used in multiplication to agree. A _complie-time_ error will be triggered if you write A * B and the number of coluns in A does not equal the...
5
2640
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object1, const MyTemplate <T> & object2); template <typename T> class MyTemplate
9
3732
by: Adam Badura | last post by:
I have code like this template<int size> big_int { /* ... */ template<int size2> friend class big_int<size2>; }; What I wanted to achive is to be able to easly convert different sized big_int's and to operate on them. But for this I need to be able to access private data of big_int. The problem is that for differenc sizes this are of
3
3758
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...
9
2063
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9243
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...
0
8241
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6795
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
6073
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
4599
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...
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
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.