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?
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?
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.
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 {publicclass ClassWrapper { }; }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.