Problem creating managed classes and namespaces in C++/CLI

I am having problems creating a managed class with namespace in C++/CLI.

I would like to do the following:

#pragma once
#include "abc.h"
#ifdef _MANAGED
#using <system.dll>
using namespace System;
using namespace System::IO;
using namespace System::Collections::Generic;
using namespace System::Globalization;
#endif

namespace Animals
    {
    public ref class Pets
        {
        Pets::Pets(){}
        };
    }

I have a couple different problems:

A) When I place this code into the .cpp file, it compiles fine. However, it appears the namespaces are not working as expected (see this question I created: Namespace not recognized in C++/CLI) The only answer listed says I must declare the classes/namespaces in the header file. But this is a problem because..

B) The compiler complains about public ref class Pets when it is placed in the header file. It says there must be a syntax error.

intellisense error:

expected a declaration

other errors:

'{' : missing function header (old-style formal list?)

syntax error: 'public'

I can't seem to find any great C++/CLI examples that show both the header and the cpp file.

So my question is: How can I make both the managed class and namespace work as expected? (ie what am I doing wrong?)

Please let me know if I need to include any more information.

Thanks in advance for your time and patience :)


在头文件中应该有前向声明。

// abc.h
#pragma once

namespace Animals
{
    public ref class Pets
    {
        Pets(); // forward declaration
        // Pets::Pets is redundant and wrong, because you are inside 
        // the class Pets
    };
}


// abc.cpp
#include "abc.h"
#ifdef _MANAGED
#using <system.dll>
using namespace System;
using namespace System::IO;
using namespace System::Collections::Generic;
using namespace System::Globalization;
#endif

namespace Animals
{
    Pets::Pets() {}  // implementation
    // Now Pets::Pets() is right, because you dont write the class... wrapper again.
}
链接地址: http://www.djcxy.com/p/59612.html

上一篇: 在C ++ / CLI代码中包含来自非托管C ++代码的头文件

下一篇: 在C ++ / CLI中创建托管类和名称空间时出现问题