473,327 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Correct syntax for class templates that declare friend functions

Hi, I am trying to figure out the correct syntax for declaring a friend
function in a class template and then defining that member after the
class. The code example below is what I am trying to get to work
properly; it generates a link error.

Thank you, Oplec.

#include <cstdlib>
#include <iostream>

using namespace std;

template<class C>
class String {
public:
friend ostream& operator<<(ostream&, const String<C>&);
};

template<class C>
ostream& operator<<(ostream& os, const String<C>& s) { return os; }

int main()
{
String<char> s;
cout << s << endl;

system("PAUSE");
}

Jul 19 '05 #1
5 8938
// Forward declaration of String, necessary for the fwd declaration
// of operator<<() below
template <class C>
class String;

// Fwd declaration
template <class C>
ostream & operator<<(ostream &, const String<C> &);
// Class interface
template <class C>
class String
{
public:
// Note the space between << and <
friend ostream & operator<< <C> (ostream &, String<C> &);
};

// operator<<() implementation.
template <class C>
ostram & operator<<(ostream & OUT, const String<C> & S)
{
// Output S;
}

On Tue, 28 Oct 2003 00:29:39 +0000, Oplec wrote:
Hi, I am trying to figure out the correct syntax for declaring a friend
function in a class template and then defining that member after the
class. The code example below is what I am trying to get to work
properly; it generates a link error.

Thank you, Oplec.

#include <cstdlib>
#include <iostream>

using namespace std;

template<class C>
class String {
public:
friend ostream& operator<<(ostream&, const String<C>&);
};

template<class C>
ostream& operator<<(ostream& os, const String<C>& s) { return os; }

int main()
{
String<char> s;
cout << s << endl;

system("PAUSE");
}


Jul 19 '05 #2
Silviu Minut wrote:
// Forward declaration of String, necessary for the fwd declaration
// of operator<<() below
template <class C>
class String;

// Fwd declaration
template <class C>
ostream & operator<<(ostream &, const String<C> &);
// Class interface
template <class C>
class String
{
public:
// Note the space between << and <
friend ostream & operator<< <C> (ostream &, String<C> &);
};

// operator<<() implementation.
template <class C>
ostram & operator<<(ostream & OUT, const String<C> & S)
{
// Output S;
}


I appreciate your trying to help me, but what you wrote gives a
compile-time error for "friend ostream & operator<< <C> (ostream &,
String<C> &);". The error just says "template-id". Note: I corrected the
definition part where you wrote "ostram".

Is there something else that was missed? I've never seen that notation
before.

Thanks, Oplec.

Jul 19 '05 #3
Oplec <an*******@anonymous.cp> wrote in message news:<aC******************@twister01.bloor.is.net. cable.rogers.com>...
Silviu Minut wrote:
// Forward declaration of String, necessary for the fwd declaration
// of operator<<() below
template <class C>
class String;

// Fwd declaration
template <class C>
ostream & operator<<(ostream &, const String<C> &);
// Class interface
template <class C>
class String
{
public:
// Note the space between << and <
friend ostream & operator<< <C> (ostream &, String<C> &); must be: friend ostream & operator<< <C> (ostream &, const String<C> &);
};

// operator<<() implementation.
template <class C>
ostram & operator<<(ostream & OUT, const String<C> & S) must be: ostream & operator<<(ostream & OUT, const String<C> & S)
{
// Output S;
}

Jul 19 '05 #4
Certainly, I was sloppy, sorry. The forward declratation is
> // Fwd declaration
> template <class C>
> ostream & operator<<(ostream &, const String<C> &);


so there should also be const String<C> in

friend ostream & operator<< <C> (ostream &, const String<C> &);

The fwd declaration and the friend declaration must have the same
prototype.

Jul 19 '05 #5
Silviu Minut wrote:
Certainly, I was sloppy, sorry. The forward declratation is

// Fwd declaration
template <class C>
ostream & operator<<(ostream &, const String<C> &);

so there should also be const String<C> in

friend ostream & operator<< <C> (ostream &, const String<C> &);

The fwd declaration and the friend declaration must have the same
prototype.


Hi,

Adding the "const" corrected the previous error. I truly appreciate your
helping me with this because I have never seen the <C> notation before
the ()'s. Is there a specific name for it? I ask because I want to
Google information about it so that I know what else to use it on.

Thank you once again, Oplec.

Jul 19 '05 #6

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

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
21
by: Sebastian Faust | last post by:
Hi, is a construction like the following possible: template<class view_model> class template_clase { protected: template_clase() {} virtual ~template_clase() {}
1
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...
8
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
2
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
9
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...
3
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...
6
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
2
by: Andy Champ | last post by:
We have a class with something that, simplified, looks like this: template <typename Tclass foo { T beyondAllReaching; // In VC2005, this works OK template <typename Ufriend void...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.