Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3806828
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:53:44+00:00 2026-05-19T14:53:44+00:00

See the code below. The drive() is in the scope , I can drive

  • 0

See the code below. The drive() is in the scope, I can drive the porsche. However, unless I uncomment the declaration of drive(), g++ gives a very weird ‘drive’ was not declared in this scope error when trying to create the functor. Why?

#include <functional>

class car {
    friend void drive(const car c);
};

//void drive(const car c);

int main() {

    car porsche;
    drive(porsche);
    std::pointer_to_unary_function<car, void> functor(drive);

    return 0;
}

UPDATE 1: I am almost satified with the answer concerning ADL, however I did tell the type of the argument of drive, it is the first template parameter, it is car:

std::pointer_to_unary_function<car, void> functor(drive);

UPDATE 2: OK, here is an even simpler code, we do not need the functor and the functional header:

class car {
    friend void drive(const car c);
};

//void drive(const car c) { }

int main() {
    car porsche;
    drive(porsche);
    void (*f)(const car);
    f = drive;
    return 0;
}

Now, I understand why the compiler cannot find drive with ADL. The reason is the same as above, but this code is not obscured by the template.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:53:44+00:00Added an answer on May 19, 2026 at 2:53 pm

    When you declare a friend function with an unqualified id in a class and that function is not a member of another class, it names a function in the nearest enclosing non-class, non-function prototype scope.

    If that function hasn’t previously been declared then the friend declaration doesn’t make that function visible in that scope.

    However, that function is visible for argument dependent lookup.

    In the expression drive(porsche);, the porsche has type car so ADL is used and the friend function can be found.

    In the expression drive there are no arguments so ADL is not performed. There is no declaration of drive visible so the lookup fails.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table defined (see code snippet below). How can I add a
See code below, for some reason it only works when I put a breakpoint
How come unserialize isn't restoring my array? See code below.. // prints a:1:{s:8:txn_type;s:32:recurring_payment_profile_cancel;} echo
EDIT: See my working code in the answers below. In brief: I have a
Update: Solved, with code I got it working, see my answer below for the
Good afternoon, I am using the code below to map a network drive in
see code below.. <div style=overflow:auto;width:250px;height:75px;border:1px solid #336699;padding-left:5px> <label style={width:250px;}><input type=checkbox name=wow[] onclick='highlight_div(this);'> PHP</label><br> <label
Please see code below: s = new Socket(nextHopIP, 3434); fileIn = new FileInputStream(fileName); fileOut
I have problem with session and query execution,please see code below. class A implements
I have problem with session and query execution, please see code below. class A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.