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 8056383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:41:12+00:00 2026-06-05T08:41:12+00:00

Im trying to define a class friend function outside the namespace like this: namespace

  • 0

Im trying to define a class friend function outside the namespace like this:

namespace A{
class window{
    private:
    int a;
    friend void f(window);
};
}

 void f(A::window rhs){
 cout << rhs.a << endl;
}

Im getting an error said that there is ambiguity. and there is two candidates void A::f(A::window); and void f(A::window). So my question is :

1) How to make the global function void f(A::window rhs) a friend of the class A::window.

EDIT: (After reading the answers)

2) why do I need to qualify the member function f inside window class to be global by doing ::f(window) ?

3) why do I need to predeclare the function f(A::window) in this particular case, whereas when the class is not a defined inside a namespace it’s okey for the function to be declared after the function is declared a friend.

  • 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-06-05T08:41:14+00:00Added an answer on June 5, 2026 at 8:41 am

    As well as adding a :: you need to forward declare it, e.g.:

    namespace A { class window; }
    
    void f(A::window);
    
    namespace A{
      class window{
        private:
        int a;
        friend void ::f(window);
      };
    }
    
    void f(A::window rhs){
      std::cout << rhs.a << std::endl;
    }
    

    Note that for this forward declaration to work you need to forward declare the class too!

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

Sidebar

Related Questions

I was trying to define a class like this: #include <library/foo.h> class my_class {
i'm trying to define a User registration class and this is the function i
In trying to compile this program: namespace MyNamespace { template<typename T> class Test {
So this a new one for me. I'm trying to define a ConfigurationSection class
I am trying to define a class. This is what I have: enum Tile
I'm trying to define base class, which contains typedef's only. template<typename T> class A
I'm stuck. I'm trying to dynamically define a class method and I can't wrap
Im trying to define a select tag in my view. My view looks like
I'm trying to define a delegate function that will return an IEnumerable. I'm having
I'm trying to create a template class, and when I define a non-member template

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.