?
Solved

overloaded operators with friend functions

Posted on 2005-03-18
11
Medium Priority
?
351 Views
Last Modified: 2012-05-05
Hi Experts,

Am trying to declare a template pointer to an object that will be passed to an overloaded friend fn.

eg:

class dec'l:

template <class ElemType>
class Set {

 private:
     listNode<ElemType> *head;

 public:
     Set ();
     ~Set ();
     Set (const Set &);
     Set & operator= (const Set &rhs);
        // some other stuff ...
    friend ostream & operator<< <ElemType>(ostream & os, const Set & s);
};

declare pointer:
 
        Set< int > *printSet;           // Set< int > here causes compiler to barf..
           if (driver_sets[k3]._set->size() > 0)
          {
          printSet = driver_sets[k3]._set;      
          cout << "Set " << driver_sets[k3]._name << "={" << (*printSet);      
          }
        else cout << "Set " << result << "={}" << endl;
        }

overloaded friend fn declaration:

ostream &operator<< <ElemType>(ostream & os, const Set & s)
{
     listNode *locator;
     if ( s.head != NULL )
        locator = s.head;
 
     os << locator->getData();
     while ( locator->getNext() != NULL )
        {
           locator = locator->getNext();
        os << "," << locator->getData();
        }
     os << "}" << endl;    
     return os;
}

 I know I'm missing something, but what is it?

Thxs!!
0
Comment
Question by:theakst
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 400 total points
ID: 13579333
>> I know I'm missing something

The template tag?

template<typename ElemType>
ostream &operator<< <ElemType>(ostream & os, const Set & s)
{
    listNode *locator;
    if ( s.head != NULL )
       locator = s.head;

     os << locator->getData();
    while ( locator->getNext() != NULL )
       {
          locator = locator->getNext();
       os << "," << locator->getData();
       }
    os << "}" << endl;    
    return os;
}
0
 
LVL 30

Expert Comment

by:Axter
ID: 13579339
Hi theakst,
You can't create a friend template friend function.

David Maisonave :-)
Cheers!
0
 

Author Comment

by:theakst
ID: 13579390

jk:

template<typename ElemType>
ostream &operator<< <ElemType>(ostream & os, const Set & s)

will still put out large error large error msg with pointer decl:      
Set< int > *printSet;  

David,

how would I go about solving the problem,  ( how do I avoid friend templating a friend functiion?)

Txs
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Axter
ID: 13579403
>>how would I go about solving the problem,  ( how do I avoid friend templating a friend functiion?)

A common method is to create member functions that are called by the global function.

Put all your logic in the member functions, and have your global function call the member functions.
0
 

Author Comment

by:theakst
ID: 13579475
eg:

class dec'l:

template <class ElemType>
class Set {

 private:
     listNode<ElemType> *head;
     void printFn( Set *printPtr );       //<--new member print fn

 public:
        // constructors ...
        // some other stuff ...
    friend ostream & operator<< <ElemType>(ostream & os, const Set & s);
};

Set.cc:

void Set::printFn( Set *printPtr );
         {
         char* name = new char[50];
         cin >> name;
         Set *printSet;
         bool _print = false;
         while ( _print == false )
         {
         for ( int k = 0; k < MAX_NUM_OF_STRUCTS; k++ )
                  if (strcmp (driver_sets[k]._name, name) == 0)
               {
             if (driver_sets[k]._set->size() > 0)
                {
                printSet = driver_sets[k]._set;      
                cout << "Set " << driver_sets[k]._name << "={" << (*printSet);       
                _print = true;
                }
             else
                _print = true;    
              }  
         for ( int k = 0; k < MAX_NUM_OF_STRUCTS; k++ )
            if (driver_sets[k]._name == "" && _print == false)
               {
             //cout << "record not found" << endl;
               driver_sets[k]._name = name;
             cout << "Set " << name << "={}" << endl;
             _print = true;            
             }         
          }}

Global fn:

printFn(  printSet ) //<--pass data to be printed here

templates are only provided in the member fn...

would this be the correct (not necessarily neatest) syntax?
0
 

Author Comment

by:theakst
ID: 13579493
what about puting the << overloaded operato into the header:

class dec'l:

template <class ElemType>
class Set {

 private:
     listNode<ElemType> *head;
     friend ostream & operator<< <ElemType>(ostream & os, const Set & s);

 public:
        // constructors ...
        // some other stuff ...
    friend ostream & operator<< <ElemType>(ostream & os, const Set & s);
};
0
 
LVL 30

Accepted Solution

by:
Axter earned 1400 total points
ID: 13579518
Here's a working example:

template <class ElemType>
class Set {

 public:
       Set(const ElemType &x):m_x(x){}
     friend ostream & operator<< (ostream & os, const Set & s)
       {
             os << s.m_x;
             return os;
       }
 private:
       ElemType m_x;
};


void main()
{
      Set <int>s(1);

      cout << s << endl;
0
 
LVL 30

Expert Comment

by:Axter
ID: 13579519
As you can see, in my example, I removed the <ElemType> that was in the friend declaration.
0
 

Author Comment

by:theakst
ID: 13579530
Kool,

Thanks for all on a friday afternoon!!

0
 
LVL 30

Expert Comment

by:Axter
ID: 13579540
It's seems I was wrong about creating a friend template global function.

The above code can also be done with the following syntax:

template <class ElemType>
class Set {
      
      public:
            Set(const ElemType &x):m_x(x){}
            friend ostream & operator<< (ostream & os, const Set & s);
      private:
            ElemType m_x;
};

template<typename ElemType>
ostream &operator<< (ostream & os, const Set<ElemType> & s)
{
      os << s.m_x;
      return os;
}

void main()
{
      Set <int>s(1);
      
      cout << s << endl;
0
 
LVL 30

Expert Comment

by:Axter
ID: 13579543
FYI:

I know there is an issue with making a friend in a template class, but I don't remember the details.
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question