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

  • Home
  • SEARCH
  • 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 6208501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:49:11+00:00 2026-05-24T05:49:11+00:00

// main.cpp const double MAX = 3.5; namespace ns { const int MAX =

  • 0
// main.cpp
const double MAX = 3.5;

namespace ns
{
   const int MAX = 3;
}

int main()
{
}

will this cause redefinition error?

I’m referring to this MSDN page, which says in the Remarks section that this is an error.

Update: I think I may miss one important statement when copying around the code.

using ns::MAX;
  • 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-05-24T05:49:12+00:00Added an answer on May 24, 2026 at 5:49 am

    No – I don’t see how that code would cause a redefinition error.

    And in fact, you can compile it and see for yourself.


    EDIT: Following up now that you’ve supplied the link to the MSDN page you mentioned…

    That MSDN page is talking about name clashes in the context of a using directive:

    If a local variable has the same name as a namespace variable, the
    namespace variable is hidden. It is an error to have a namespace
    variable with the same name as a global variable.

    Here’s an example of a local variable hiding a namespace variable that’s been brought into scope by a using directive:

    namespace ns
    {
       const int MAX = 3;
    }
    
    using namespace ns;
    
    int main()
    {
       int MAX = 4; // local
       int test = MAX;   // test is 4, because the local variable is used 
                         // as the namespace variable is hidden
    }
    

    The inclusion of the using directive brings all of the names declared within the ns namespace into scope. However, when I assign the value of MAX to test, it’s the local variable MAX that is used in the assignment because the namespace variable MAX is hidden. The local variable takes precedence and hides the namespace variable.

    Now here’s an example of a clash between a namespace variable and a global variable.
    Consider this amended piece of code (and you can see it compile here):

    const double MAX = 3.5;
    
    namespace ns
    {
       const int MAX = 3;
    }
    
    using namespace ns;
    
    int main()
    {
       int test = MAX;
    }
    

    Again, the using directive brings ns:MAX into scope, and the global variable MAX is also in scope.

    When I go to use the variable called MAX in main(), the compiler reports an error because the name MAX is now ambiguous: it has no way of knowing which MAX we are referring to, as there are two non-local MAXs to choose from: neither has any precedence.

    prog.cpp: In function ‘int main()’:
    prog.cpp:13: error: reference to ‘MAX’ is ambiguous
    prog.cpp:2: error: candidates are: const double MAX
    prog.cpp:6: error:                 const int ns::MAX
    prog.cpp:13: error: reference to ‘MAX’ is ambiguous
    prog.cpp:2: error: candidates are: const double MAX
    prog.cpp:6: error:                 const int ns::MAX
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How print format string passed as argument ? example.cpp: #include <iostream> int main(int ac,
I have the following source code in main.cpp: #include <iostream> #include <iomanip> int main()
I've got a c++ app, with the following main.cpp: 1: #include <stdio.h> 2: #include
I created a new HalloWorld Makefile Project. There is a HalloWorld.cpp with my main
main.c (with all the headers like stdio, stdlib, etc): int main() { int input;
I've created some mathematical functions that will be used in main() and by member
Here is what I did, I want to gracefully handle this exception: code_snippet: my.cpp
Why does this code: class A { public: explicit A(int x) {} }; class
code: #include<iostream> using namespace std; template<class T, int N> class point { T coordinate[N];
I have a long double constant that I am setting either as const or

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.