Problem with Friend

From: Larry Lindstrom (nobody_at_aracnet.com)
Date: 03/31/04


Date: Wed, 31 Mar 2004 19:09:04 GMT

Hi Experts:

    Win2k, MSVC6.0.

    I use friend functions. I'm doing something stupid and
I'm not seeing it.

    When I try to use a private class variable in a function
that is a friend of that class the compiler is complaining
about :

    cannot access private member declared in class 'DATABASE_RECORD'

    I have two database objects about which is said, "IBPP
objects are indeed kind of smart-pointers to object
interfaces." I haven't used an auto-pointer, so I'm
keeping each in it's little class, and passing references
to that, in hopes of not loosing ownership. So that might
be complicating things.

    Here are some fragments of the code. Pulled in order from
the preprocessor's ".i" file to simplify things.

                       ------

class DATABASE_RECORD;

class TRANSACTION_RECORD
{
public:
     bool submit_sql(const string *sql_string_ptr,
                     string *message_string_ptr);
private:
     DATABASE_RECORD *database_ptr;
};

class DATABASE_RECORD
{
public:
     friend bool
         TRANSACTION_RECORD::submit_sql(const string *sql_string_ptr,
                                           string *message_string_ptr);

private:
     IBPP::Database database_id; <=== "See declaration" reference
};

bool TRANSACTION_RECORD::submit_sql(const string *sql_string_ptr,
                                        string *message_string_ptr)
{
     IBPP::Statement st1 =
         IBPP::StatementFactory(database_ptr->database_id, <== Error line
                                                    transaction);
}

                       ------

error C2248: 'database_id' : cannot access private member declared in
class 'DATABASE_RECORD'
: see declaration of 'database_id'

                       ------

    I've looked at some of the 4570 posts with "friend" in the
title on Google's archives, but I didn't see this.

    One suggestion was to make the "friend" declaration the first
one. So I switched the positions of the two classes in the
header, added forward declarations, and got a "use of undefined
type 'TRANSACTION_RECORD'" compiler error at the "friend"
declaration.

    This has to be so stupid. What am I doing wrong?

                                               Thanks
                                               Larry



Relevant Pages

  • Re: friend class in template
    ... This template takes 3 type parameters, the first of which is named ... The friend declaration attempts to declare the class ... the friend declaration doesn't do what the author apparently ... thought it would do (at least not on a conforming compiler). ...
    (microsoft.public.vc.language)
  • Re: Class templates and friend function templates
    ... Our compiler has a "Microsoft bugs" mode that also ... > form of the operator declared in the friend declaration. ... Microsoft compilers seem to treat friend function declarations as ...
    (microsoft.public.vc.language)
  • Re: Making a header to include other header (C++) Et Al.
    ... | #ifndef A_H ... an forward declaration, ... times the compiler can see the declaration - What matters, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Mixing Mananged and Unmanaged in an Unmanaged class
    ... The workaround is to define the function outside the class. ... So the compiler decides whether the function is managed or unmanaged at ... gc class), and the class contains a gcrootas a private memeber, ... managed/unmanged around the m_Test declaration. ...
    (microsoft.public.dotnet.languages.vc)
  • VS2003 Compiler Bug
    ... I'm not sure if this is a bug being caused by visual studio, ... I get the compiler error bc30456...which is ... declared...and basically if I change its declaration from private to ... fail..and upon switching it back to private build again. ...
    (microsoft.public.dotnet.languages.vb)