help with namespace/friend error

Discussion in 'C++' started by Joe, Oct 29, 2009.

  1. Joe

    Joe Guest

    Shouldn't I be able to use a friend function like below? I'm getting
    the following compiler errors in XCode 3 …

    'test' was not declared in this scope
    'double AB::test(double, double)' should have been declared inside 'AB'

    Seems like this might have something to do w/ my namespaces. I've tried
    to include everything I thought relevant to this problem.

    --- BN.h ---
    namespace AB {
    class BN
    {
    public:
    friend double test(double a, double b);
    };
    }

    --- BN.cc ---
    // error on next line … 'double AB::test(double, double)' should have
    been declared inside 'AB'
    double AB::test(double a, double b)
    {
    return
    };

    --- AL.h ---
    namespace AB {
    class AL : public BL
    {
    public:
    AL();
    };
    }

    --- AL.cc ---
    using namespace std;

    // constructor
    AB::AL::AL() : BL() {
    // error on next line … 'test' was not declared in this scope
    test();
    };

    In case the above looks strange, I've double checked for any typos, and
    it's all right (left out forward declarations, headers, etc...). This
    used to compile in XCode 2.
     
    Joe, Oct 29, 2009
    #1
    1. Advertising

  2. Joe wrote:

    > Shouldn't I be able to use a friend function like below? I'm getting
    > the following compiler errors in XCode 3 …
    >
    > 'test' was not declared in this scope
    > 'double AB::test(double, double)' should have been declared inside 'AB'
    >
    > Seems like this might have something to do w/ my namespaces. I've tried
    > to include everything I thought relevant to this problem.
    >
    > --- BN.h ---
    > namespace AB {
    > class BN
    > {
    > public:
    > friend double test(double a, double b);
    > };
    > }
    >
    > --- BN.cc ---
    > // error on next line … 'double AB::test(double, double)' should have
    > been declared inside 'AB'
    > double AB::test(double a, double b)
    > {
    > return
    > };
    >
    > --- AL.h ---
    > namespace AB {
    > class AL : public BL
    > {
    > public:
    > AL();
    > };
    > }
    >
    > --- AL.cc ---
    > using namespace std;
    >
    > // constructor
    > AB::AL::AL() : BL() {
    > // error on next line … 'test' was not declared in this scope
    > test();
    > };
    >
    > In case the above looks strange, I've double checked for any typos, and
    > it's all right (left out forward declarations, headers, etc...). This
    > used to compile in XCode 2.

    It's wrong because a friend function declaration just declares a function as
    a friend, but it will never introduce a visible name into an enclosing scope
    (the name is introduced, but it's *not* visible). Argument dependent lookup
    is a special case, in which the function name introduced by that declaration
    is visible in its namespace.

    This means, you have to declare it yourself in AB before you define it
    outside of it.
     
    Johannes Schaub (litb), Oct 29, 2009
    #2
    1. Advertising

  3. Joe

    Joe Guest

    >
    > This means, you have to declare it yourself in AB before you define it
    > outside of it.



    How do I declare this before I define it? I thought that's what I was
    doing, declaring it within the namespace AB of the class BN, and of
    course it's defined elsewhere.

    Guess I still don't' understand about introducing a visible name into
    an enclosing scope (which I thought it was).
     
    Joe, Oct 29, 2009
    #3
  4. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Joe wrote:

    >>
    >> This means, you have to declare it yourself in AB before you define it
    >> outside of it.

    >
    >
    > How do I declare this before I define it? I thought that's what I was
    > doing, declaring it within the namespace AB of the class BN, and of
    > course it's defined elsewhere.
    >
    > Guess I still don't' understand about introducing a visible name into
    > an enclosing scope (which I thought it was).


    // BN.h
    namespace AB {
    class BN
    {
    public:
    friend double test(double a, double b);
    };
    double test(double a, double b);
    }
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (GNU/Linux)

    iEYEARECAAYFAkrqv00ACgkQG6NzcAXitM9V7ACfQ+/KIsAkKsz297+btbV+5l8+
    X1UAnApoL5M7za2hOup0WhXmoQ5UVQrJ
    =nfwQ
    -----END PGP SIGNATURE-----
     
    Michael Tsang, Oct 30, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Grigoriev
    Replies:
    6
    Views:
    2,585
    Kevin Goodsell
    Sep 12, 2003
  2. Tim Partridge
    Replies:
    2
    Views:
    623
    Nick Hounsome
    Jan 8, 2004
  3. Tim Partridge
    Replies:
    1
    Views:
    721
    Sumit Rajan
    Jun 2, 2004
  4. Pierre Yves

    friend template in namespace error

    Pierre Yves, Jan 11, 2010, in forum: C++
    Replies:
    2
    Views:
    688
    Balog Pal
    Jan 11, 2010
  5. Peter
    Replies:
    2
    Views:
    311
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page