mans
mans

Reputation: 18228

Namespace name in c++

I am trying to create a namespace in c++ in the following way:

namespace MyCompany.Library.Myproduct {

public ref class ClassWrapper
{

};
 }

I am getting error:

  Error 1   error C2059: syntax error : '.' ClassWrapper.h  7   1    MyCompany.Library.Myproduct 

Why can not I have . in namespace?

Edit1

This namespace definition is in a c++/cli and would be used in c# code. in C# this namespace is valid, but it seems it is not valid in c++. How can define c# compatible namespaces in c++/cli code?

Upvotes: 4

Views: 2821

Answers (2)

pro100tom
pro100tom

Reputation: 165

Now, in 2023 it is possible to achieve what you want; with ::. You need to make sure the C++ Language Standard is set to at least C++17.

Then this code would compile and work:


    namespace MyCompany::Library::Myproduct {
        public class ClassWrapper
        {
        };
    }

Upvotes: 0

Xaqq
Xaqq

Reputation: 4396

You're not allowed to use the dot in namespace name. However, you can have nested namespaces.

namespace Boap
{
  namespace Lib
  {
    namespace Prod
    {
      class Llama
      {
      public:
        Llama()
        {
          std::cout << "hi" << std::endl;
        }
      };
    }
  }
};

And instanciate your llama this way:

Boap::Lib::Prod::Llama l;

AFAIK namespace and classname follow the same namming rules than variables. A variable's name cannot start with a numeric character, and the same apply to class / namespace's names. The same goes for ".".

EDIT: The following is pure assumptions, because I have no knowledge of C# or windows CLI. Does it make sense that a nested namespace in C++ (eg Boap::Lib) would be translated into Boap.Lib in C#? Maybe it's just as simple as that.

Upvotes: 8

Related Questions