Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #3432

templated operator()<<

From Ruben Safir <ruben@mrbrklyn.com>
Newsgroups comp.lang.c++
Subject templated operator()<<
Date 2011-04-11 23:39 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <io03f9$qsv$1@reader1.panix.com> (permalink)

Show all headers | View raw


as I understand things, the operator<<() needs to be external as to the 
class and then declared as a friend of the class.  I'm not sure if I 
understand why that is the case.

I did this with a class called MESSAGE and had not trouble with the 
general implementation.  However, when I tried to do this with a 
templated class that generally looks like, although this is not compiling

namespace stats{

   template<class T>
   class Distribution {
	template<T> friend std::ostream & operator<<(std::ostream &, const Distribution<T>&);
	public:
		/* ====================  LIFECYCLE     ======================================= */
		Distribution (T descr, int occurances = 0);
		Distribution():freq(NULL),occurances(NULL){};
	//	Distribution ( const Distribution &other );   /* copy constructor */
//		~Distribution ();                            /* destructor       */
		/* ====================  ACCESSORS     ======================================= */
		T description(){ return freq;}
	        int population() { return occurances; }	
		/* ====================  MUTATORS      ======================================= */
		void increase_occ(){ ++occurances; std::cout << "description " << freq << " occurances " << occurances << std::endl; }
		void descrease_occ(){ --occurances; }
		/* ====================  OPERATORS     ======================================= */
		//Distribution& operator = ( const Distribution &other ); /* assignment operator */
		int operator()(){
		   return freq;
		}
		bool operator==(Distribution &tmp){
		   if(this->freq == tmp.freq)
		      return true;
		   return false;
		}

		bool operator<(Distribution &tmp){
		   if(freq < tmp.freq)
		      return true;
		   return false;
		}



	protected:
		/* ====================  DATA MEMBERS  ======================================= */
	private:
		/* ====================  DATA MEMBERS  ======================================= */
		T freq;  //description of how many times found in a List
		int occurances; //description of how many times a frequency was found in a list
}; /* -----  end of class Distribution  ----- */


template<typename T>
std::ostream & operator << ( std::ostream & os, const Distribution<T> & obj )
      {
	 T desc = obj.description();
	 int pop  = obj.population();
	 os << "The Identification of " << desc << " was seens " << pop ;
	 return os;
      }		/* -----  end of function operator <<  ----- */


Not this is not compiling because:

test_del.cpp|90| error: passing ‘const stats::Distribution<int>’ as ‘this’ argument of ‘T stats::Distribution<T>::description() [with T = int]’ discards qualifiers

which is on the call for the use of the << operator on the class.  before I templated 
the operaotr<<(), I got a warning that I was using a non-templated function.  I've done some research and this
peoblem seems to have been common, but I don't know of any solutions, and I don't understand what the implementation
issue is.

Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

templated operator()<< Ruben Safir <ruben@mrbrklyn.com> - 2011-04-11 23:39 +0000
  Re: templated operator()<< LR <lruss@superlink.net> - 2011-04-11 20:07 -0400
  Re: templated operator()<< "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-12 02:29 +0200
    Re: templated operator()<< Ruben Safir <ruben@mrbrklyn.com> - 2011-04-12 01:59 +0000
      Re: templated operator()<< "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-12 04:01 +0200
        Re: templated operator()<< Ruben Safir <ruben@mrbrklyn.com> - 2011-04-12 02:37 +0000
        Re: templated operator()<< Ruben Safir <ruben@mrbrklyn.com> - 2011-04-12 02:43 +0000
          Re: templated operator()<< "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-12 05:25 +0200
            Re: templated operator()<< Michael Doubez <michael.doubez@free.fr> - 2011-04-12 00:17 -0700
      Re: templated operator()<< Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-04-12 08:23 +0200

csiph-web