#### Archived

This topic is now archived and is closed to further replies.

# Container passing

## Recommended Posts

kelaklub    160
Could someone shove me in the right direction, cuz I am going nuts trying to figure this one out. I can't seem to figure out how to pass an STL container to a function, through value or by reference, or whatever, I can't figure it out...
#include <iostream>
#include <stack>
using namespace std;

template <typename tType>
class cl_3dPoint
{
public:
tType x, y, z;

cl_3dPoint() : x(0.0),
y(0.0),
z(0.0){}

inline void fn_setXYZ(tType X, tType Y, tType Z)
{
x = X;
y = Y;
z = Z;
}
};

template <typename tType>
void fn_doSomething(stack<cl_3dPoint<tType> > stackVertices)
{
}

int main()
{
cl_3dPoint<float> point[3];
point[0].fn_setXYZ(1, 4, 0);
point[1].fn_setXYZ(4, 12, 0);
point[2].fn_setXYZ(2, 7, 0);

stack< cl_3dPoint<float> > stackVertices;
for (int i = 0; i < 3; i++)
{
stackVertices.push(point[i]);
}

fn_doSomething(stackVertices);

return 0;
}


Thank you for your time. [edited by - kelaklub on January 16, 2004 12:42:16 PM] [edited by - kelaklub on January 16, 2004 12:58:02 PM]

##### Share on other sites
petewood    819
is there a problem with the code?

##### Share on other sites
kelaklub    160
I actually have this code in seperate files, the class is in its own header file because it is a template class. The function''s prototype is in it''s own header file, and the function implementation is in its cpp file. That all compiles perfectly. But when I make the call to the function, everything goes berserk. I am using VisualC6.

##### Share on other sites
jonnii    122
it seems like your passing it by value, have you tried passing it by reference?

##### Share on other sites
fizban75    130
Um, you''re creating an array of 3 points, then you loop over an array of 10 points, pushing them onto the stack. Perhaps that''s your problem???

##### Share on other sites
kelaklub    160
I tried value, reference, you name it. All I know is that whatever I was doing was not correct, because it wasn''t compiling. Oh sorry about that for loop, I fixed it. I was just copying and pasting my code and was trying to cut it down a little, must have missed the for loop. But that is not the problem.

##### Share on other sites
SiCrane    11839
Try pasting the actual code instead of cut down code. Maybe the actual problem is somewhere that you didn''t think was significant.

##### Share on other sites
kelaklub    160
Your right you guys, the totality of the code is more important than it's cut and pasted parts. I should not have been trying to look for an easy answer. Sorry to bother you guys, i'll figure this out. Thank you for your time. In fact what I just posted compiles fine, it's just not compiling in my project where the code is isolated in different files. I'll figure this out. Thank you.

[edited by - kelaklub on January 16, 2004 1:00:55 PM]

##### Share on other sites
SiCrane    11839
I just noticed the part of the post where you said that the function''s prototype is in the header and the function''s definition is in a source file. This isn''t kosher in MSVC 6. You have to include the implementation of a template (both class templates and function templates) inside the header files.