I am new to namespaces and tried to use this from C ++ Primer
#include<iostream>
namespace Jill
{
double bucket;
double fetch;
struct Hill{ };
}
double fetch;
int main()
{
using namespace Jill;
Hill Thrill;
double water = bucket;
std::cin>> fetch;
std::cin>> ::fetch;
std::cin>> Jill::fetch;
std::cout<<"fetch is "<<fetch;
std::cout<<"::fetch is "<< ::fetch;
std::cout<<"Jill::fetch is "<< Jill::fetch;
}
int foom()
{
Jill::Hill top;
Jill::Hill crest;
}
When the line marked //<<<<<<<<<<<<//is not commented out, I get the expected results. those. The variable localhides globaland Jill::fetch. But when I comment on this, there are 2 selections on the left. global fetchand Jill::fetch. And the compiler gives an error
namespaceTrial1.cpp:17:13: error: reference to ‘fetch’ is ambiguous
namespaceTrial1.cpp:9:8: error: candidates are: double fetch
namespaceTrial1.cpp:5:9: error: double Jill::fetch
namespaceTrial1.cpp:20:26: error: reference to ‘fetch’ is ambiguous
namespaceTrial1.cpp:9:8: error: candidates are: double fetch
namespaceTrial1.cpp:5:9: error: double Jill::fetch
My question is , why is the compiler getting confused , does this lead to ambiguity? Why it doesn’t accept fetchas soon Jill::fetchas I added using namespace Jillat the beginningmain()
using Jill::fetch; main, . using Jill::fetch , . , local fetch. [ ?] using declaration , , using directive doesnt?