danijar
danijar

Reputation: 34185

How to share the scope shared between extended namespaces?

Please take a look at the following code. First, a namespace holding a class is created. Then, the namespace is extended and the class we defined before is used. Here is a live demo.

// File one
namespace system {
    class module {
        // ...
    };
}

// File two including file one
namespace system {
    struct entry {
        entry(module *module);
        module *module;
    };
}

Compiling this under gcc 4.8.1 without the permissive flag produces two compiler errors. Explicitly mentioning the namespace like system::module solves the problem, even though it is the same namespace we're already in.

error: declaration of ‘system::module* system::entry::module’ [-fpermissive]
    module *module;
            ^
error: changes meaning of ‘module’ from ‘class system::module’ [-fpermissive]
   class module {
         ^

Why is this code not standard conform? What other options do I have except explicitly mentioning the namespace every time.

Upvotes: 1

Views: 71

Answers (3)

2785528
2785528

Reputation: 5566

// File one
namespace system {
    class module {
        // ...
    };
}

// File two including file one
namespace system {
    struct entry {
        entry(module *module);
        module *module;
    };
}

where I cannot really come up with more descriptive names

Naming is one of the most difficult challenges programmer's face, and yet, when shown a way (or 2 or n), it will forever be easier.

I offer the following - from a Coding standard I have long used.

class and namespace names are Capitalized, at least the 1st char

variable names are lower case (or camel case with lower case 1st char)


// File one
namespace System {
    class Module {
        // ...
    };
}

// File two including file one
namespace System {
    struct Entry {
        Entry(Module* module);
        Module* module;
    };
}

So?

Search for and read some of the coding standards. They exist for a reason. Once you learn one good idea, it will serve the rest of your career.

Upvotes: 1

jxh
jxh

Reputation: 70402

As already pointed out in another answer, the compiler is complaining that you have changed the semantics of an identifier from a type to a variable name (module).

You should give your types a name that will make them easily distinguishable from variables. You should give member variables names that can be easily distinguished from regular variables. This increases code readability and maintainability. And, it has the side-effect of making the problem you face a non-issue:

// File one
namespace system {
    class module_type {
        // ...
    };
}

// File two including file one
namespace system {
    struct entry {
        entry(module_type *module);
        module_type *module_;
    };
}

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

In this statement

module *module;

you redefined name module.

So in the member function declaration you have to use elaborated type name

entry( class module *module);

In any case it is a bad idea to redefine names such a way.

Upvotes: 1

Related Questions