简体   繁体   English

C++/CLI 中无法识别命名空间

[英]Namespace not recognized in C++/CLI

I asked this question recently: create a namespace in c++/cli?我最近问了这个问题: create a namespace in c++/cli? and so I am trying to create my own namespace in c++/cli.所以我试图在 c++/cli 中创建我自己的命名空间。

But when I use the same namespace in two separate files (.cpp), the namespace is clearly not recognized as being the same because I get errors when I try to reference the other class in the other file.但是当我在两个单独的文件 (.cpp) 中使用相同的命名空间时,命名空间显然不会被识别为相同,因为当我尝试在另一个文件中引用另一个 class 时出现错误。

Here's basically what I have:这基本上是我所拥有的:

Pets.cpp:宠物.cpp:

namespace Animals
   {
   public ref class Pets 
     {
     public:
        List<Dog> ^vDogs;

     Pets::Pets()
        {
        vDogs = gcnew List<Dog^>();
        }

     void Pets::DoSomething()
        {
        }
     };
   }

Dog.cpp:狗.cpp:

namespace Animals
   {
   public ref class Dog 
     {

     Dog::Dog()
        {
        }

     void Dog::DoSomething()
        {
        }
     };
   }

Other information:其他信息:

1) Files are in the same folder 1)文件在同一个文件夹中

2) Files were added to an existing solution in a different folder 2) 文件被添加到不同文件夹中的现有解决方案中

3) I also tried using namespace Animals in either file but I get an error saying namespace does not exist. 3) 我也尝试在任一文件中using namespace Animals ,但我收到一条错误消息,指出命名空间不存在。

4) I am using Visual Studio 2010 (fyi in case someone has a way to fix but I need to do something specific in VS) 4)我正在使用 Visual Studio 2010(仅供参考,以防有人有办法修复,但我需要在 VS 中做一些特定的事情)

So my question is: What do I need to do in order for the namespace to be recognized?所以我的问题是:我需要做什么才能识别命名空间?

Please let me know what other information is needed in order for the problem to be solved.请让我知道需要什么其他信息才能解决问题。

Thanks in advance for your time and patience: :)提前感谢您的时间和耐心::)

The C++/CLI compiler is stuck with the build model of traditional C and C++ compilers. C++/CLI 编译器被传统 C 和 C++ 编译器的构建 model 卡住了。 Formulated at a time when a kilobyte of memory took as much space as a shoebox.当一千字节的 memory 占用与鞋盒一样多的空间时制定。 It is a single-pass compiler that processes one.cpp file at a time.它是一次处理一个.cpp 文件的单遍编译器。 With a linker to glue the bits together.使用 linker 将钻头粘合在一起。

Which means that you'll have to use traditional header files to declare your classes and the #include directive at the top of your source code file to include it.这意味着您必须使用传统的 header 文件来声明您的类,并在源代码文件顶部使用 #include 指令来包含它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM