5

In the example below, is it necessary to use namespace A{} in the source file or is it redundant as it is already been done in the header file?

// header file Foo.h

namespace A
{
    class Foo
    {
        Foo();
    };
}

// source file Foo.cpp

#include "Foo.h"

namespace A
{
    Foo::Foo() {}
}
6
  • 1
    You may find that namespace A; is an easier way of doing this as it doesn't indent the entire rest of the file. Commented Nov 20, 2019 at 19:06
  • 2
    It's also much more readable as you get larger projects. Please continue this practice. Commented Nov 20, 2019 at 19:06
  • 1
    @tadman namespace A; is not proper syntax. What is it supposed to mean? Commented Nov 20, 2019 at 19:09
  • @uneven_mark Guess it's an incomplete using statement Commented Nov 20, 2019 at 19:10
  • 1
    @raven Yeah using namespace A; would work as long as there are no name collisions with the global namespace. Commented Nov 20, 2019 at 19:17

1 Answer 1

7

It's necessary, but you can do the following instead:

// source file Foo.cpp

#include "Foo.h"

A::Foo::Foo() {}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.