using namespace & overloaded function - c++

I don't understand why I get the error "ambiguous call to overloaded function".
Before "main", I declared to use namespace "second".
My expected output is:
this is the first foo
this is the second foo
this is the second foo
#include <iostream>
using namespace std;
namespace first {
void foo() {
cout << "this is the first foo" << endl;
}
}
namespace second {
void foo() {
cout << "this is the second foo" << endl;
}
}
void foo() {
cout << "this is just foo" << endl;
}
using namespace second;
void main() {
first::foo();
second::foo();
foo();
}

Before "main", I declared to use namespace "second".
When you do this, second::foo is introduced into the global namespace, then for foo(); both second::foo and ::foo are valid candidates.
You can specify that you want to call the global foo explicitly, i.e.
::foo();
Or use using-declaration inside main() instead of using-directive, e.g.
int main() {
using second::foo;
first::foo();
second::foo();
foo();
}

The reason this is happening is because of your: using namespace second;
When you do this you qualify the function foo() from the second namespace into the global scope. In the global scope, there exists another function foo() with the exact same signature. Now, your calls to first::foo() and second::foo() are fine. However, when you get to your call to foo(), the compiler doesn't know whether it should call just foo() or second::foo() since they are ambiguous, and at this point they are both in the global namespace.

Your using namespace second; is precisely the reason you get the error. It practically tells the compiler to treat second::foo as if it was in the global namespace. So now you have two foos there.
By the way, void main is not valid C++. It must be int main.

Firstly, replace void main() { } with
int main() {
return 0;
}
Secondly, using namespace second; should be inside main() from where you want to call particular namespace, not globally.
Finally, If you are using ::scope resolution operator then you don't have to mention using namespace second in the main(), use either one.
Just do like below
int main() {
first::foo(); /* first foo will be called */
second::foo(); /* second foo() will be called */
foo(); /* global foo() will be called */
return 0;
}

Related

C++ Namespace Convention

I am new to C++.
I have a file that has the following:
namespace A {
namespace B {
function foo() {
}
}
function bar() {
}
}
and another file has the following:
namespace A {
namespace C {
// Call foo() and bar() here.
}
}
If I want to call foo and bar inside of namespace C, should I include their absolute namespace?
A::B::foo();
A::bar();
or I don't have to include the namespace A since they are all inside A?
B::foo();
bar();
This is a name lookup question.
So if you've read this, you'll know the difference between Unqualified Lookup and Qualified Lookup
So obviously, unqualified lookup is valid here, but if you want to do some work like disambiguation, you may use qualified lookup. So, qualified lookup is always valid when unqualified lookup is valid.(If you can use qualified lookup).
Just little addition. As Constructor said, qualified lookup always generates well-formed code, but that code does not always produce an expected result. Consider this disambiguation example
#include <iostream>
void foo();
void foo(float);
namespace Boo {
using ::foo;
void foo(int a) { std::cout << __PRETTY_FUNCTION__ << " is here!\n"; }
}
void foo() { std::cout << __PRETTY_FUNCTION__ << " is here!\n"; }
void foo(int a) { std::cout << __PRETTY_FUNCTION__ << " is here!\n"; }
void foo(float) { std::cout << __PRETTY_FUNCTION__ << " is here!\n"; }
using namespace Boo;
int main(int argc, char *argv[])
{
foo(); // no error!
Boo::foo(); // still same
//foo(3); // aw, there are three of them. Bad, bad `using namespace`!
Boo::foo(3);
Boo::foo(3.f);
return 0;
}
namespace Boo propagates name foo to be Boo::foo. We had defined foo() after namespace declaration, but prototype declaration required to be known before.
Now imagine that Boo is std, foo prototypes are something like abs or sin from standard header and are declared that way. Header implementations are allowed to use trick from above. Result of re-definition of names used in standard namespace is described as undetermined, because depending of if using was in implementation or not, the program would use standard or user-defined function with no regard to qualified name.
TL;DR: Result of qualified lookup is always correct but is not always well-determined if reserved names are not respected.

C++ error: 'count_of_function_calls' was not declared in this scope

hi I am new to c++ and I am stuck in a question. I am a beginner, please help me, that you.
#include <iostream>
using namespace std;
int dostuff ()
{
return 2 + 3;
}
void fun ()
{
count_of_function_calls++;
}
int main()
{
void fun ();
void fun ();
void fun();
cout << "Function fun was called" << count_of_function_calls << "times";
}
Many, many problems, you should definitely read a C++ book or reread some tutorials
Where did you define count_of_function_calls?
Nowhere, that's why the compiler is complaining. You always have to declare variables before you use them:
int count_of_function_calls = 0;
Note that in your case, because you want to value of count_of_function_calls to be incremented for each function call, you should declare it as a global variable (this is not recommended, consider using something else).
A global variable is declared outside of any scope, in your case, you could for example defined it just above void fun ().
void fun (); declares a function (called fun), taking no arguments and returning void. It doesn't call the function fun. If you want to call a function, you don't have to specify the return type:
//Call function 'fun'
fun();
I think you forgot to define global variable count_of_function_calls
For example
#include <iostream>
using namespace std;
int count_of_function_calls;
int dostuff ()
{
return 2 + 3;
}
void fun ()
{
count_of_function_calls++;
}
//...
And the function calls must look like
fun();
This
void fun ();
is a function declaration. It is not a call of the function.

using symbols from a namespace in one function but not another in the same file

I have to define two functions, say, foo() and bar() in the same namespace and the same file. For the definition of the first, foo(), I want to use all symbols of, say, namespace other, but don't want symbols from namespace other to be automatically in scope for my other function, bar(). Is this possible? How?
(note: I don't want to know about alternative "solutions" either avoiding this problem of mitigating it, such as namespace o=other etc.)
Yes, it is possible:
void foo()
{
using namespace abc;
....
}
or
void foo()
{
using abc::x;
using abc::y;
using abc::z;
....
}
#include <iostream>
void quux() { std::cout << "root\n"; }
namespace other {
void quux(int x = 0) { std::cout << "other\n"; }
}
namespace taxes {
void foo() {
using namespace other;
quux(3);
};
void bar() {
quux();
}
}
int main() {
taxes::foo();
taxes::bar();
}
Live example
Note that quux in bar would be ambiguous if it could see other::quux, but it cannot.
On the other hand, this does not give you access to namespace other in the 'head' of the function foo (the parameters etc), but that is a rare requirement. There might be a solution involving inline namespaces or the like, but probably not worth the confusion.

Anonymous Namespace Ambiguity

Consider the following snippet:
void Foo() // 1
{
}
namespace
{
void Foo() // 2
{
}
}
int main()
{
Foo(); // Ambiguous.
::Foo(); // Calls the Foo in the global namespace (Foo #1).
// I'm trying to call the `Foo` that's defined in the anonymous namespace (Foo #2).
}
How can I refer to something inside an anonymous namespace in this case?
You can't. The standard contains the following section (§7.3.1.1, C++03):
An unnamed-namespace-definition behaves as if it were replaced by
namespace unique { /* empty body */ }
using namespace unique;
namespace unique { namespace-body }
where all occurrences of unique in a
translation unit are replaced by the
same identifier and this identifier
differs from all other identifiers in the entire program.
Thus you have no way to refer to that unique name.
You could however technically use something like the following instead:
int i;
namespace helper {
namespace {
int i;
int j;
}
}
using namespace helper;
void f() {
j++; // works
i++; // still ambigous
::i++; // access to global namespace
helper::i++; // access to unnamed namespace
}
While Georg gives standard-complient, correct, right, and respectable answer, I'd like to offer my hacky one - use another namespace within the anonymous namespace:
#include <iostream>
using namespace std;
namespace
{
namespace inner
{
int cout = 42;
}
}
int main()
{
cout << inner::cout << endl;
return 0;
}
The only solution I can think of that doesn't modify the existing namespace arrangement is to delegate main to a function in the anonymous namespace. (main itself is required to be a global function (§3.6.1/1), so it cannot be in an anonymous namespace.)
void Foo() // 1
{
}
namespace
{
void Foo() // 2
{
}
}
namespace { // re-open same anonymous namespace
int do_main()
{
Foo(); // Calls local, anonymous namespace (Foo #2).
::Foo(); // Calls the Foo in the global namespace (Foo #1).
return 0; // return not optional
}
}
int main() {
return do_main();
}
The only real way is to put the code you want to access that namespace within the namespace itself. There's no way to resolve to the unnamed namespace otherwise, since it has no identifier you can give it to solve the ambiguous resolution problem.
If your code is inside the namespace{} block itself, the local name gets priority over the global one, so a Foo() will call the Foo() within your namespace, and a ::Foo() will call the namespace at global scope.
Just rename the local namespace function.

How do I reference an external C++ namespace from within a nested one?

I have two namespaces defined in the default/"root" namespace, nsA and nsB. nsA has a sub-namespace, nsA::subA. When I try referencing a function that belongs to nsB, from inside of nsA::subA, I get an error:
undefined reference to `nsA::subA::nsB::theFunctionInNsB(...)'
Any ideas?
Use global scope resolution:
::nsB::TheFunctionInNsB()
#include <stdio.h>
namespace nsB {
void foo() {
printf( "nsB::foo()\n");
}
}
namespace nsA {
void foo() {
printf( "nsA::foo()\n");
}
namespace subA {
void foo() {
printf( "nsA::subA::foo()\n");
printf( "calling nsB::foo()\n");
::nsB::foo(); // <--- calling foo() in namespace 'nsB'
}
}
}
int main()
{
nsA::subA::foo();
return 0;
}
Need more information to explain that error. The following code is fine:
#include <iostream>
namespace nsB {
void foo() { std::cout << "nsB\n";}
}
namespace nsA {
void foo() { std::cout << "nsA\n";}
namespace subA {
void foo() { std::cout << "nsA::subA\n";}
void bar() {
nsB::foo();
}
}
}
int main() {
nsA::subA::bar();
}
So, while specifying the global namespace solves your current problem, in general it is possible to refer to symbols in nsB without it. Otherwise, you'd have to write ::std::cout, ::std::string, etc, whenever you were in another namespace scope. And you don't. QED.
Specifying the global namespace is for situations where there's another nsB visible in the current scope - for instance if nsA::subA contained its own namespace or class called nsB, and you want to call ::nsbB:foo rather than nsA::subA::nsB::foo. So you'd get the error you quote if for example you have declared (but not defined) nsA::subA::nsB::theFunctionInNsB(...). Did you maybe #include the header for nsB from inside namespace subA?