Solved

Non-local class, namespace scope, function name must be unqualified.

Posted on 2013-01-24
3
498 Views
Last Modified: 2013-01-24
Hello Experts!

Found such a description and I do not really understand it...

However, you can define a function in a friend declaration. The class must be a non-local class. The function must have namespace scope, and the function name must be unqualified. The following example demonstrates this:

1. what is non-local class?
2. The function must have namespace scope- what does it mean?
3. Function name must be unqualified- what does it mean?


class A {
  void g();
};

void z() {
  class B {
    friend void f() { }; // error
  };
}

class C {
  friend void A::g() { } // error
  friend void h() { }
};

Open in new window



Thank you!
0
Comment
Question by:panJames
  • 3
3 Comments
 
LVL 22

Expert Comment

by:ambience
ID: 38813560
1. what is non-local class?

A class declared inside a function or method is non-local. It can not be used outside the defining scope.
0
 
LVL 22

Expert Comment

by:ambience
ID: 38813574
3. Function name must be unqualified?

These are qualified function names

  friend void ::h() {
  }
  friend void Test::h() {
  }

This is not

  friend void h() {
  }

WRT - friends that this means is

void f() {}

namespace Test {

      void f() {}

      class C
      {
                // definition must be unqualified
            friend void h() {
                  cout << "From h" << std::endl;
            }
            friend void Test::f(); // qualified declaration is allowed
            friend void ::f(); // qualified declaration is allowed

      public:
            void method()
            {
                  h();
            }
      };

}
0
 
LVL 22

Accepted Solution

by:
ambience earned 500 total points
ID: 38813594
2. The function must have namespace scope- what does it mean?

This should explain

namespace Test {

	void h();  // h is declared in namespace - not defined!

	class C 
	{
	public:
                // this is inside C but its definition of h in the namespace's scope
		friend void h() { 
			cout << "From h" << std::endl;
		}
	};

}


int main() 
{
	Test::h();
}

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question