如果有人能指出一些示例代码或项目的方向,我将不胜感激,这些代码或项目在头文件 (.h) 和代码文件 (.cpp) 之间拆分了 C++/CLI 类,它使用泛型及其成员函数。我在编译时遇到了一些麻烦,因为我试图在语法上处理泛型,就像我在非托管 C++ 项目中使用模板的方式一样,并且似乎找不到在拆分文件中使用泛型的任何好的示例。是的,这是我第一次尝试在 C++/CLI 中构建项目。
这是我的代码,如果有人能发现错误:
C++/CLI .dll:
.h:
#ifndef __ManagedCPlusPlus_h__
#define __ManagedCPlusPlus_h__
//#include "CPlusPlusArray.h"
using namespace System;
class CPlusPlusArray;
namespace ManagedCPlusPlusArray {
generic<typename S2, typename I2>
public ref class ManagedCPlusPlusArray
{
public:
ManagedCPlusPlusArray(S2 size);
void SetItem(S2 index, I2 value);
I2 GetItem(S2 index);
S2 Size(void);
virtual ~ManagedCPlusPlusArray(void);
private:
CPlusPlusArray* m_cppa;
};
}
#endif
.cpp:
// This is the main DLL file.
#include <STDIO.H>
//#include "stdafx.h"
#include "ManagedCPlusPlusArray.h"
#include "CPlusPlusArray.h"
using namespace System::Runtime::InteropServices;
namespace ManagedCPlusPlusArray
{
generic<typename S2, typename I2>
ManagedCPlusPlusArray<S2,I2>::ManagedCPlusPlusArray(S2 size)
{
this->m_cppa = new CPlusPlusArray(size);
}
generic<typename S2, typename I2>
void ManagedCPlusPlusArray<S2,I2>::SetItem(S2 index, I2 value)
{
this->m_cppa->SetItem(index, value);
}
generic<typename S2, typename I2>
I2 ManagedCPlusPlusArray<S2,I2>::GetItem(S2 index)
{
return this->m_cppa->GetItem(index);
}
generic<typename S2, typename I2>
S2 ManagedCPlusPlusArray<S2,I2>::Size(void)
{
return this->m_cppa->Size();
}
generic<typename S2, typename I2>
ManagedCPlusPlusArray<S2,I2>::~ManagedCPlusPlusArray(void)
{
delete this->m_cppa;
this->m_cppa = NULL;
}
}
C++ .dll:
.h:
#ifndef __CPlusPlusArray_h__
#define __CPlusPlusArray_h__
//more about this in reference 1
#ifdef CPLUSPLUSARRAY_EXPORTS
#define CPLUSPLUSARRAY_API __declspec(dllexport) // export DLL information
#else
#define CPLUSPLUSARRAY_API __declspec(dllimport) // import DLL information
#endif
template <typename I, typename S> class CPLUSPLUSARRAY_API CPlusPlusArray {
private:
S _size;
I _items[];
public:
CPlusPlusArray(S size);
void SetItem(S index, I value);
I GetItem(S index);
S Size(void);
~CPlusPlusArray(void);
};
#endif
.cpp:
#include <iostream>
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include "CPlusPlusArray.h"
#ifdef _MANAGED
#pragma managed(push, off)
#endif
BOOL APIENTRY DllMain(
HMODULE /*hModule*/,
DWORD ul_reason_for_call,
LPVOID /*lpReserved*/)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#ifdef _MANAGED
#pragma managed(pop)
#endif
template <typename I, typename S>
CPlusPlusArray<I, S>::CPlusPlusArray(S size)
{
this->_size = size;
this->_items = new I[this->_size];
}
template <typename I, typename S>
void CPlusPlusArray<I, S>::SetItem(S index, I value)
{
this->_items[index] = val;
}
template <typename I, typename S>
I CPlusPlusArray<I, S>::GetItem(S index)
{
return this->_item[index];
}
template <typename I, typename S>
S CPlusPlusArray<I, S>::Size(void)
{
return this->_size;
}
template <typename I, typename S>
CPlusPlusArray<I, S>::~CPlusPlusArray(void)
{
delete[] this->_items;
}