Note that there are some explanatory texts on larger screens.

plurals
  1. POStandard structure of a C++/CLI program which uses generics?
    primarykey
    data
    text
    <p>I would be grateful if anyone could point me in the direction of some sample code or project that has a C++/CLI class split between a header (.h) and code file (.cpp), which uses generics with its member functions. I am having some trouble compiling, as I am attempting to treat generics, syntactially, the way I would templates in an unmanaged C++ project, and can't seem to find any good examples of generics being used in a split file. And yes, this is my first time trying to build a project in C++/CLI. </p> <p>Here's my code, if anyone can spot the errors:</p> <p><strong>The C++/CLI .dll:</strong></p> <pre><code>.h: #ifndef __ManagedCPlusPlus_h__ #define __ManagedCPlusPlus_h__ //#include "CPlusPlusArray.h" using namespace System; class CPlusPlusArray; namespace ManagedCPlusPlusArray { generic&lt;typename S2, typename I2&gt; 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 &lt;STDIO.H&gt; //#include "stdafx.h" #include "ManagedCPlusPlusArray.h" #include "CPlusPlusArray.h" using namespace System::Runtime::InteropServices; namespace ManagedCPlusPlusArray { generic&lt;typename S2, typename I2&gt; ManagedCPlusPlusArray&lt;S2,I2&gt;::ManagedCPlusPlusArray(S2 size) { this-&gt;m_cppa = new CPlusPlusArray(size); } generic&lt;typename S2, typename I2&gt; void ManagedCPlusPlusArray&lt;S2,I2&gt;::SetItem(S2 index, I2 value) { this-&gt;m_cppa-&gt;SetItem(index, value); } generic&lt;typename S2, typename I2&gt; I2 ManagedCPlusPlusArray&lt;S2,I2&gt;::GetItem(S2 index) { return this-&gt;m_cppa-&gt;GetItem(index); } generic&lt;typename S2, typename I2&gt; S2 ManagedCPlusPlusArray&lt;S2,I2&gt;::Size(void) { return this-&gt;m_cppa-&gt;Size(); } generic&lt;typename S2, typename I2&gt; ManagedCPlusPlusArray&lt;S2,I2&gt;::~ManagedCPlusPlusArray(void) { delete this-&gt;m_cppa; this-&gt;m_cppa = NULL; } } </code></pre> <p><strong>The C++ .dll:</strong></p> <pre><code>.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 &lt;typename I, typename S&gt; 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 &lt;iostream&gt; #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers #include &lt;windows.h&gt; #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 &lt;typename I, typename S&gt; CPlusPlusArray&lt;I, S&gt;::CPlusPlusArray(S size) { this-&gt;_size = size; this-&gt;_items = new I[this-&gt;_size]; } template &lt;typename I, typename S&gt; void CPlusPlusArray&lt;I, S&gt;::SetItem(S index, I value) { this-&gt;_items[index] = val; } template &lt;typename I, typename S&gt; I CPlusPlusArray&lt;I, S&gt;::GetItem(S index) { return this-&gt;_item[index]; } template &lt;typename I, typename S&gt; S CPlusPlusArray&lt;I, S&gt;::Size(void) { return this-&gt;_size; } template &lt;typename I, typename S&gt; CPlusPlusArray&lt;I, S&gt;::~CPlusPlusArray(void) { delete[] this-&gt;_items; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload