Tomáš Zato
Tomáš Zato

Reputation: 53358

Define namespace::int class

I kinda hoped that use of namespace will allow me do define classes with names of already existing classes - so the namespace will be the way to distinguish them. I seek this because I have server protocol having some data types so I need classes that implement them, and their conversion to bytes.
This is what I did, and it produced errors:

#ifndef _PACKET_DATA_TYPES
#define _PACKET_DATA_TYPES
namespace mcp_t {
    class mcp_t::int {  //ERROR: expected an identifier

    }
}
#endif

If this is not possible, namespace seems a bit useless here - I'll be forced to use mcp_int instead anyway.

Upvotes: 1

Views: 1007

Answers (2)

NPE
NPE

Reputation: 500953

int is a keyword, and keywords cannot be used to name user-defined types (even inside namespaces).

I am afraid you'll have to call your type something other than int.

Upvotes: 7

Andy Prowl
Andy Prowl

Reputation: 126582

You should not fully qualify the name of the class when providing its definition, and you should not use keywords as class names:

namespace mcp_t {
    class my_int {
        // ...
    };
}

mcp_t::my_int x;

Upvotes: 3

Related Questions