Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 836
mans Avatar asked Oct 31 '25 00:10

mans


1 Answers

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.

like image 102
Xaqq Avatar answered Nov 02 '25 13:11

Xaqq