简体   繁体   中英

Namespace not recognized in C++/CLI

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

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.

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.

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