Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > help with namespace/friend error

Reply
Thread Tools

help with namespace/friend error

 
 
Joe
Guest
Posts: n/a
 
      10-29-2009
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.


 
Reply With Quote
 
 
 
 
Johannes Schaub (litb)
Guest
Posts: n/a
 
      10-29-2009
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.
 
Reply With Quote
 
 
 
 
Joe
Guest
Posts: n/a
 
      10-29-2009
>
> 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).

 
Reply With Quote
 
Michael Tsang
Guest
Posts: n/a
 
      10-30-2009
-----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-----

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0xffc Thread 0x228 DBC 0x437b94 Jet'. ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr bazzer ASP .Net 0 03-30-2006 03:16 PM
Error connecting to SQLExpress 2005 locally (error: 26 - Error Locating Server/Instance Specified) hfk0 ASP .Net 2 03-27-2006 08:43 PM
ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x8fc Thread 0x934 DBC 0x437b94 Jet'. ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr bazzer ASP .Net 1 03-24-2006 04:20 PM
ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x8fc Thread 0x934 DBC 0x437b94 Jet'. ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr bazzer ASP .Net 0 03-24-2006 02:22 PM
HELP! HELP! HELP! Opening Web Application Project Error =?Utf-8?B?dHJlbGxvdzQyMg==?= ASP .Net 0 02-20-2004 05:16 PM



Advertisments