Thread: compiler doesn't recognize my friend

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

    compiler doesn't recognize my friend

    Hi all,

    Code:
    #include "CallbackTimer.h" // declares callbackfun
    
    namespace nsScheduler{
    
    class Scheduler : boost::noncopyable
    {
    	friend class Job; 
    	friend class WorkerThread;
    	friend VOID CALLBACK callbackfun( PVOID param, BOOLEAN timer_or_wait_fired );
           //I want to allow that function access to member function onTimerTick()
    
           void onTimerTick();
           //.....
    public:
           //...
    };
    
    }//namespace

    Code:
    //CallbackTimer.h
    
    VOID CALLBACK callbackfun( PVOID param, BOOLEAN timer_or_wait_fired );
    
    namespace nsScheduler{
    
    class Scheduler;
    
    class CallbackTimer :boost::noncopyable
    {
    //...
    }
    
    
    
    //CallbackTimer.cpp
    
    #include "CallbackTimer.h"
    
    #include "Scheduler.h"
    
    namespace nsScheduler{
    
    //...
    }
    
    VOID CALLBACK 
    callbackfun( PVOID param, BOOLEAN timer_or_wait_fired )
    {
    	( (nsScheduler::Scheduler*)param)->onTimerTick();
    }
    but MSVC says:

    .\CallbackTimer.cpp(48) : error C2248: 'nsScheduler::Scheduler::onTimerTick' : cannot access private member declared in class 'nsScheduler::Scheduler'




    Does anybody has an idea why my friend decleration is ignored?

    Thank you in advance!
    Last edited by pheres; 12-05-2008 at 10:53 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It would be good if you posted the smallest and simplest program that demonstrates the error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Well I had a linker that didn't recognize my uncle, but that's a whole different problem
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    here it is


    Code:
    #define _WIN32_WINNT 0x0500
    
    #define WIN32_LEAN_AND_MEAN
    #include <Windows.h>
    #undef WIN32_LEAN_AND_MEAN
    
    VOID CALLBACK callbackfun( PVOID param, BOOLEAN timer_or_wait_fired );
    
    namespace nsScheduler{
    
    	class Scheduler;
    
    	class CallbackTimer
    	{
    
    		Scheduler& mrScheduler;
    
    	public:
    
    		CallbackTimer( Scheduler& scheduler )
    			:mrScheduler(scheduler)
    		{}
    	};
    
    class Scheduler
    {
    	friend VOID CALLBACK callbackfun( PVOID param, BOOLEAN timer_or_wait_fired );
    
    	void onTimerTick()
    	{}
    };
    
    }//ns
    
    VOID CALLBACK 
    callbackfun( PVOID param, BOOLEAN timer_or_wait_fired )
    {
    	( (nsScheduler::Scheduler*)param)->onTimerTick();
    }
    
    int main()
    {}



    ------ Build started: Project: TestProg, Configuration: Debug Win32 ------
    Compiling...
    main.cpp
    c:\workspace-eclipse\testprog\testprog\main.cpp(23) : warning C4512: 'nsScheduler::CallbackTimer' : assignment operator could not be generated
    c:\workspace-eclipse\testprog\testprog\main.cpp(14) : see declaration of 'nsScheduler::CallbackTimer'
    c:\workspace-eclipse\testprog\testprog\main.cpp(38) : error C2248: 'nsScheduler::Scheduler::onTimerTick' : cannot access private member declared in class 'nsScheduler::Scheduler'
    c:\workspace-eclipse\testprog\testprog\main.cpp(29) : see declaration of 'nsScheduler::Scheduler::onTimerTick'
    c:\workspace-eclipse\testprog\testprog\main.cpp(26) : see declaration of 'nsScheduler::Scheduler'
    Build log was saved at "file://c:\workspace-eclipse\TestProg\TestProg\Debug\BuildLog.htm"
    TestProg - 1 error(s), 1 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's not clear from what you posted: you declare a friend function nsScheduler::callbackfun, and then declare a function ::callbackfun later?

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    hell,

    Code:
    class Scheduler
    {
    	friend VOID CALLBACK ::callbackfun( PVOID param, BOOLEAN timer_or_wait_fired );
    did the trick.

    thanks to brewbuck's uncle!

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Indeed. Make it ::callbackfun in the friend declaration and it will work.


    EDIT: Ah, late.

    EDIT2: Well, the normative text about this was annoyingly hard to find, and in a highly unintuitive place. (In particular, it was neither in 3.4, name lookup, nor in 11.4, friends.)
    Quote Originally Posted by ISO C++ 2003, 7.3.1.2/3
    When looking for a prior declaration of a class or a function declared as a friend, and when the name of the friend class or function is neither a qualified name nor a template-id, scopes outside the innermost enclosing namespace scope are not considered.
    Last edited by CornedBee; 12-05-2008 at 11:20 AM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Problem with friend class declaration in a namespace
    By Angus in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2008, 01:29 PM
  3. Problem with friend functions in templated linked list
    By Strait in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2005, 04:24 PM
  4. compiler gone haywire?!
    By patito12 in forum C Programming
    Replies: 5
    Last Post: 11-13-2004, 06:18 AM
  5. Declare a template class as a friend?
    By AH_Tze in forum C++ Programming
    Replies: 11
    Last Post: 05-19-2004, 09:24 PM