Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Confused about friend operators, namespaces, and private members...

0 views
Skip to first unread message

Robert Heller

unread,
Nov 17, 2009, 4:56:08 PM11/17/09
to
I am using gcc 3.4.6 under CentOS 4.8 (RHEL 4.8):
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.6/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-java-awt=gtk
--host=i386-redhat-linux
Thread model: posix
gcc version 3.4.6 20060404 (Red Hat 3.4.6-11)

And I am getting errors like this:

./../../C++/ParserClassesBoost/TrackGraph.cc: In function `Parsers::TrackGraph::Transform2D* operator*(const Parsers::TrackGraph::Transform2D&, const Parsers::TrackGraph::Transform2D&)':
./../../C++/ParserClassesBoost/TrackGraph.h:747: error: `float Parsers::TrackGraph::Transform2D::matrix[3][3]' is private
./../../C++/ParserClassesBoost/TrackGraph.cc:675: error: within this context
./../../C++/ParserClassesBoost/TrackGraph.h:747: error: `float Parsers::TrackGraph::Transform2D::matrix[3][3]' is private
./../../C++/ParserClassesBoost/TrackGraph.cc:675: error: within this context
./../../C++/ParserClassesBoost/TrackGraph.h:747: error: `float Parsers::TrackGraph::Transform2D::matrix[3][3]' is private
./../../C++/ParserClassesBoost/TrackGraph.cc:676: error: within this context
make[1]: *** [libMRRParserClassesBoost_la-TrackGraph.lo] Error 1
make[1]: Leaving directory `/home/heller/Deepwoods/ModelRRSystem/Linux/C++/ParserClassesBoost'
make: *** [all] Error 2

Here is the relevant fragment of TrackGraph.cc:

using namespace Parsers;

TrackGraph::Transform2D* operator * (const TrackGraph::Transform2D& t1, const TrackGraph::Transform2D& t2)
{
TrackGraph::Transform2D *new_t = new TrackGraph::Transform2D;

for(int i=0; i < 3; i++)
for(int j=0; j < 3; j++) {
float sum = 0.0;
for(int k=0; k < 3; k++)
sum += t1.matrix[j][k] * t2.matrix[k][i];
new_t->matrix[j][i] = sum;
}

return new_t;
}

And of TrackGraph.h:

namespace Parsers {

class TrackGraph {
public:
class Transform2D {
private:
/** Transform matrix.
*/
float matrix[3][3];
/** Fuzz factor.
*/
const static float FUZZ = .00001;
public:
/** Matrix multiplication.
*/
friend Transform2D* operator * (const Transform2D& t1,const Transform2D& t2);
};
};

};

I don't understand these error messages. They didn't happen before I
added the namespace code. I would *like* to embed my code in a
namespace and not put random stuff into the global namespace, but
either gcc 3.4.6 does not handle namespaces well when dealing with
friend operators or I am doing something wrong, but I don't know what
that could be. For various reasons I don't want to install a newer
version of gcc (I want to stay binary compatible with the rest of my
system).

--
Robert Heller -- 978-544-6933
Deepwoods Software -- Download the Model Railroad System
http://www.deepsoft.com/ -- Binaries for Linux and MS-Windows
hel...@deepsoft.com -- http://www.deepsoft.com/ModelRailroadSystem/

Thomas Maeder

unread,
Nov 18, 2009, 1:24:20 AM11/18/09
to
Robert Heller <hel...@deepsoft.com> writes:

> Here is the relevant fragment of TrackGraph.cc:

Too hard to parse (for me anyway).

Please reduce to the minimal amount of code that allows your audience
to see what you are seeing.

crim...@googlemail.com

unread,
Nov 18, 2009, 8:05:37 AM11/18/09
to
On 17 ноя, 23:56, Robert Heller <hel...@deepsoft.com> wrote:

> ./../../C++/ParserClassesBoost/TrackGraph.h:747: error: `float Parsers::TrackGraph::Transform2D::matrix[3][3]' is private
> ./../../C++/ParserClassesBoost/TrackGraph.cc:675: error: within this context

> ...


> I don't understand these error messages.  

Variable matrix is private for Transform2D, but you try to access it
from TrackGraph class.

Robert Heller

unread,
Nov 18, 2009, 9:16:19 AM11/18/09
to
At Wed, 18 Nov 2009 05:05:37 -0800 (PST) "crim...@googlemail.com" <crim...@googlemail.com> wrote:

>
> On 17 =D0=BD=D0=BE=D1=8F, 23:56, Robert Heller <hel...@deepsoft.com> wrote:
>
> > ./../../C++/ParserClassesBoost/TrackGraph.h:747: error: `float Parsers::T=


> rackGraph::Transform2D::matrix[3][3]' is private

> > ./../../C++/ParserClassesBoost/TrackGraph.cc:675: error: within this cont=
> ext
> > ...
> > I don't understand these error messages. =C2=A0


>
> Variable matrix is private for Transform2D, but you try to access it
> from TrackGraph class.

No, I am not! Variable matrix is private for Transform2D and is accessed
by *friend* operator * (friend of Transform2D). This code compiles just
fine *without* the namespace declarations, but fails to compile with the
namespace declarations.

Robert Heller

unread,
Nov 18, 2009, 2:46:24 PM11/18/09
to

OK, I figured out that I needed to qualify the friend operators with the
namespace -- appearently things are ambigious otherwise. The error
message is a little confusing (to me).

0 new messages