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