MESSAGE
DATE | 2011-04-12 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] [billy.donahue@gmail.com: Re: templated operator()<<]
|
For tonights discussion
Tuesday night NYLXS C++ workshop - 7:00PM 1163 east 15th street Brooklyn, USA 11230
Q or B to Avenue J
----- Forwarded message from Billy -----
Date: Tue, 12 Apr 2011 14:34:17 -0400 From: Billy To: Ruben Safir Subject: Re: templated operator()<<
ohhhhhhhhhh! you were already inside a template class! Didn't realize that. This is why it's important to give the full situation.
template friend std::ostream& operator<<(std::ostream&, const Distribution&);
Yes, this is what you do, yes, fine.
template std::ostream& operator<<(std::ostream& os, const Distribution& obj) { T desc = obj.description(); int pop = obj.population(); os << "The Identification of " << desc << " was seen " << pop ; return os; }
..Okay, so here's the deal about this:
86 template friend std::ostream& operator<<(std::ostream&, const Distribution&); test_del.cpp:86: error: declaration of ?class T? test_del.cpp:44: error: shadows template parm ?class T?
You're accidentally declaring a new function base template here, taking a class T as parameter. The symbol 'T' is already used as a parameter of the enclosing class template class Distribution {...}; SO when you use 'T' in the argument list later (the 'const Distribution&' argument), it's warning you that you'll be using a different 'T', one that's local to the friend definition.
Here's an incorrect fix, which might be useful to think about: 86 template friend std::ostream& operator<<(std::ostream&, const Distribution&);
This grants friendship from all overloads of operator<<(std::ostream&,const Distribution&) for any U to all Distribution classes. That is to say, operator<<(std::ostream&, const Distribution&) can, in its body, look at private members of the class Distribution, which is not what you want.
What YOU want, is for a single specialized template function, called:
template std::ostream& operator<<(std::ostream&, const Distribution&)
to have access to the privates of the Distribution. This is not a family of functions, this is one function.
Anyway, since you're using only public accessors in the body of operator<<, you can get by without the friend line altogether. Just take it out.
On Tue, Apr 12, 2011 at 2:06 PM, Ruben Safir wrote:
> The only one that is needed to compile is test_del.cpp > > Ruben > -- > http://www.mrbrklyn.com - Interesting Stuff > http://www.nylxs.com - Leadership Development in Free Software > > So many immigrant groups have swept through our town that Brooklyn, like > Atlantis, reaches mythological proportions in the mind of the world - RI > Safir 1998 > > http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI > Safir 2002 > > "Yeah - I write Free Software...so SUE ME" > > "The tremendous problem we face is that we are becoming sharecroppers to > our own cultural heritage -- we need the ability to participate in our own > society." > > "> I'm an engineer. I choose the best tool for the job, politics be > damned.< > You must be a stupid engineer then, because politcs and technology have > been attached at the hip since the 1st dynasty in Ancient Egypt. I guess > you missed that one." > > ? Copyright for the Digital Millennium >
-- ?n??uop ?ll?q
----- End forwarded message -----
|
|