Problem with Friend

From: Larry Lindstrom (
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.



     bool submit_sql(const string *sql_string_ptr,
                     string *message_string_ptr);
     DATABASE_RECORD *database_ptr;

     friend bool
         TRANSACTION_RECORD::submit_sql(const string *sql_string_ptr,
                                           string *message_string_ptr);

     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


error C2248: 'database_id' : cannot access private member declared in
: 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"

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


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). ...
  • 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 ...
  • 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, ...
  • 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. ...
  • 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. ...