Correct syntax for class templates that declare friend functions

O

Oplec

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");
}
 
S

Silviu Minut

// 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;
}
 
O

Oplec

Silviu said:
// 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.
 
S

Silviu Minut

Certainly, I was sloppy, sorry. The forward declratation is

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.
 
O

Oplec

Silviu said:
Certainly, I was sloppy, sorry. The forward declratation is




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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top