Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #3432
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!news-out.readnews.com!transit3.readnews.com!postnews.google.com!news1.google.com!newsfeed2.dallas1.level3.net!news.level3.com!panix!not-for-mail |
|---|---|
| From | Ruben Safir <ruben@mrbrklyn.com> |
| Newsgroups | comp.lang.c++ |
| Subject | templated operator()<< |
| Date | Mon, 11 Apr 2011 23:39:21 +0000 (UTC) |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Lines | 72 |
| Message-ID | <io03f9$qsv$1@reader1.panix.com> (permalink) |
| NNTP-Posting-Host | www2.mrbrklyn.com |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | 8bit |
| X-Trace | reader1.panix.com 1302565161 27551 96.57.23.82 (11 Apr 2011 23:39:21 GMT) |
| X-Complaints-To | abuse@panix.com |
| NNTP-Posting-Date | Mon, 11 Apr 2011 23:39:21 +0000 (UTC) |
| To | mrbrklyn@panix.com, ruben@mrbrklyn.com |
| X-Blackjet | Blackjet is a Yankee Fan |
| X-DRMisTHEFT | Use GNU Linux today |
| X-From | A Dark Cloud |
| X-LOCATION | Brooklyn NY - Forget abou' it! |
| X-NYLXS | Really - yah think computers are supposed to be broken? |
| User-Agent | Pan/0.133 (House of Butterflies) |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.c++:3432 |
Show key headers only | 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
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