Namespace not recognized in C++/CLI

懵懂的女人 提交于 2019-12-31 02:10:51

问题


I asked this question recently: create a namespace in c++/cli? and so I am trying to create my own namespace in 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.

Here's basically what I have:

Pets.cpp:

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

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

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

Dog.cpp:

namespace Animals
   {
   public ref class Dog 
     {

     Dog::Dog()
        {
        }

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

Other information:

1) Files are in the same folder

2) Files were added to an existing solution in a different folder

3) I also tried using namespace Animals in either file but I get an error saying namespace does not exist.

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)

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! :)


回答1:


The C++/CLI compiler is stuck with the build model of traditional C and C++ compilers. Formulated at a time when a kilobyte of memory took as much space as a shoebox. It is a single-pass compiler that processes one .cpp file at a time. With a linker to glue the bits together.

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.



来源:https://stackoverflow.com/questions/6696598/namespace-not-recognized-in-c-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!