The problem with using namespace XXX and using XXX:xxx;.

using namespace Exercise; does not cause ival"s redeclaration,
but using Exercise::ival; instead leads to why redeclaration is;

such as the following code:

-sharpinclude <iostream>

using namespace std;
namespace Exercise
{
    int ivar = 0;
    double dvar = 0;
    const int limit = 1000;
}

// using namespace Exercise;
using Exercise::ivar;
int ivar = 0;
CPP
Jun.07,2022

The

namespace is to solve the problem of naming conflicts, but your using Exercise::ivar; is equivalent to the ivar variable in Exercise , that is, it is not ivar and using Exercise::ivar that conflicts, but you declare using Exercise::ivar
the following code will be fine, because there is no declare using Exercise::ivar

again.
-sharpinclude <iostream>

using namespace std;
namespace Exercise
{
    int ivar = 0;
    double dvar = 0;
    const int limit = 1000;
}

// using namespace Exercise;
int ivar = 0;
int main()
{
    cout<<Exercise::ivar;
    cout<<ivar;
    return 0;
}
Menu