Archived

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

kelaklub

Container passing

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
DOOOOOOOOOOOH, that was it. I can''t believe I did that. My classes were template based and they went into their own header file along with implementation. Why didn''t I do the same thing with my template function? Doh, Doh, Doh, Doh, Doh, Doh. Thanks.

Share this post


Link to post
Share on other sites