Note that there are some explanatory texts on larger screens.

plurals
  1. POpassing pointer to int array into template function
    primarykey
    data
    text
    <p>I have been stuck on this all afternoon, and even tried the following search: "c++ passing pointer to array via function", and can't find a working answer, so here is my question.</p> <p>Before I begin, please, this is NOT an OpenGL question, this is an array pointer passing question.</p> <p>Also, don't get '....' (4 dot) mixed up with '...' (3 dot). There is a lot of code I am skipping over with '....' (4 dot), the ... (3 dots) are the ellipse parameter for variable number of paramters passed to a function.</p> <p>These are the snippets from the four files involed:</p> <p>OpenGL.h</p> <pre><code>class OpenGL { .... (other unrelated stuff) public: int * iPixelFormatAttribList[]; &lt;---------- .... </code></pre> <p>utilities.h</p> <pre><code>template &lt;typename T&gt; void LoadArray (T * [], int, ...); &lt;-------- </code></pre> <p>utilities.cpp</p> <pre><code>// Dynamically Load Array. template &lt;typename T&gt; void LoadArray (T * Dest [], int count, ...) { &lt;------- va_list list; va_start(list,count); T * temp [] = new T [count]; for (int cnt = 0; cnt &lt; count; cnt++) * Dest[cnt] = va_arg(list, T); va_end(list); Dest = temp; delete [] temp; } </code></pre> <p>OpenGL.cpp</p> <pre><code>void OpenGL::V3_SetupPixelFormat() { ..... LoadArray ( iPixelFormatAttribList, 15, &lt;--------- WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, WGL_SUPPORT_OPENGL_ARB, GL_TRUE, WGL_DOUBLE_BUFFER_ARB, GL_TRUE, WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, WGL_COLOR_BITS_ARB, 32, WGL_DEPTH_BITS_ARB, 24, WGL_STENCIL_BITS_ARB, 8, 0 // End of attributes list ); </code></pre> <p>Ok, So, here what I am trying to do. I know that in a class definition, (OpenGL.h, the OpenGL class), that space is not allocated for any members, and because when I create it, I do not know how many paramters I am going need for an array, I need to find a way to dynamically allocate and setup the list so I can pass it into later OpenGL calls.</p> <p>(Another reason I decided to setup a dynamic loading list like this was because there are several arrays involved like this, loading arrays, and I may also need this same type of functionality later with doubles and what not for vector data. Creating this utility template seems a forward thinking way to go.)</p> <p>This all <strong>LOOKS</strong> Ok, and in fact, it compiles clean, but it does not link. I get the following:</p> <pre><code>**** Internal Builder is used for build **** windres --use-temp-file -i..\res\resource.rc -o..\res\resource_rc.o g++ -D_SS_DEBUG_ -ID:\Dev\Projects\Eclipse\OpenGL3\res -O0 -g3 -Wall -c -fmessage-length=0 -std=gnu++0x -o src\OpenGL.o ..\src\OpenGL.cpp g++ -D_SS_DEBUG_ -ID:\Dev\Projects\Eclipse\OpenGL3\res -O0 -g3 -Wall -c -fmessage-length=0 -std=gnu++0x -o src\main.o ..\src\main.cpp g++ -D_SS_DEBUG_ -ID:\Dev\Projects\Eclipse\OpenGL3\res -O0 -g3 -Wall -c -fmessage-length=0 -std=gnu++0x -o src\Utilities.o ..\src\Utilities.cpp g++ -D_SS_DEBUG_ -ID:\Dev\Projects\Eclipse\OpenGL3\res -O0 -g3 -Wall -c -fmessage-length=0 -std=gnu++0x -o src\App.o ..\src\App.cpp g++ -D_SS_DEBUG_ -ID:\Dev\Projects\Eclipse\OpenGL3\res -O0 -g3 -Wall -c -fmessage-length=0 -std=gnu++0x -o src\Win.o ..\src\Win.cpp g++ -o OpenGL3.exe src\main.o src\Win.o src\Utilities.o src\OpenGL.o src\App.o ..\res\resource_rc.o -lopengl32 -lglew32 -lglu32 -lkernel32 -lgdi32 -lcomdlg32 -luser32 src\OpenGL.o: In function `ZN6OpenGL19V3_SetupPixelFormatEv': D:\Dev\Projects\Eclipse\OpenGL3\Debug/../src/OpenGL.cpp:54: undefined reference to `void LoadArray&lt;int&gt;(int**, int, ...)' collect2: ld returned 1 exit status Build error occurred, build is stopped Time consumed: 3000 ms. </code></pre> <p>The key line to me looks like: </p> <pre><code> undefined reference to `void LoadArray&lt;int&gt;(int**, int, ...)' </code></pre> <p>What this seems to tell is the way I am calling the function:</p> <pre><code>LoadArray ( iPixelFormatAttribList, 15, WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, WGL_SUPPORT_OPENGL_ARB, GL_TRUE, .... </code></pre> <p>And the way I am defining the template function:</p> <pre><code> template &lt;typename T&gt; void LoadArray (T * [], int, ...); </code></pre> <p>and:</p> <pre><code> template &lt;typename T&gt; void LoadArray (T * Dest [], int count, ...) { </code></pre> <p>don't match. I get that much.</p> <p>What I don't get is how to adjust the template (Or the call) so that they match so it can link (i.e. I got my function signatures all screwed up.)</p> <p>The basic idea to this is, I call LoadArray with an array pointer, the element count, and the list of elements, and it modifies the pointer so that it points to a new list containing the array elements.</p> <p>I am sure there are fancy C++ ways to do this, but I want to understand how to make this work as it seems it should here. (i.e. it would help me to learn if I know what exactily I was doing wrong here, rather then a redirect solution that won't teach me what I did wrong, I know enough to know I am missing something in the syntax of passing an array pointer like this, I just can't figure out the right black magic to get it right.)</p> <p>Thanks.</p>
    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