Skip to main content
GameDev.net gamedev.net
🔒 Locked

STL

Started by kelaklub Jan 4, 2004 at 1:50 PM 11 replies 1.7k views
Original Post
kelaklub
kelaklub
I have a very basic undestanding of STL, but I am trying to get better. I have come across a situation that has me stumped. I created this simple template point class....
namespace nm_geometric
{
template <typename tType>
class Point
{
public:
	tType x, y, z;
	
	~Point()
	{
	}
	
        Point() : x(0.0),
		  y(0.0),
		  z(0.0){}
	
	Point(const Point& param) : x(param.x),
		                    y(param.y),
		                    z(param.z){}
	
        Point(const tType X, const tType Y, const tType Z)
	{
	    x = X;
	    y = Y;
	    z = Z;
	}
};
}

int main()
{
  using namespace nm_geometric;

  Point<float> p1(1, 4, 0);
  Point<float> p2(7, 9, 0);
  Point<float> p3(4, 2, 0);

  /* HERE LIES THE PROBLEM. I HAVE NO CLUE HOW TO
     PASS THESE POINTS INTO AN STL CONTAINER LIKE
     STACK, LIST, VECTOR, ETC. SO THAT I CAN DO ALL
     THAT COOL STL STUFF LIKE SORT, COUNT, ETC. PLEASE
     HELP
  */
  //stack<Point> vertices;


  return 0;
}
THANK YOU. [edited by - kelaklub on January 4, 2004 3:11:51 PM]
Prosper/LOADED
Prosper/LOADED

std::stack< Point<float> > myStack;

^
|
|
+--- do not forget this space

jdhardy
jdhardy
You need to write the assignment operator, as well as operator< for sorting (or a sperate comparison function). Then std::vector< Point<float> > should do it. Note that the space between > > is signifigant.

[edited by - ze_jackal on January 4, 2004 3:05:04 PM]
kelaklub
kelaklub
Holy moly, I did that exact same thing you suggested, I just did not have that space. Could you please tell me why that is significant. OOOOPS, I forget to add it in(i''ll fix that), but I encapsulated my class inside a namespace, and it is apparently giving me warnings, when I take out the namespace everything compiles just fine, any reason for this. Thanks.
Nervo
Nervo
quote:
Original post by kelaklub
Holy moly, I did that exact same thing you suggested, I just did not have that space. Could you please tell me why that is significant. OOOOPS, I forget to add it in(i'll fix that), but I encapsulated my class inside a namespace, and it is apparently giving me warnings, when I take out the namespace everything compiles just fine, any reason for this. Thanks.


It is significant that you put a space so as to not confuse the compiler into thinking that you are using the >> operator which would most commonly be used with std::cin, fstream on and on etc.

[edited by - nervo on January 4, 2004 3:13:37 PM]
Well, R2D22U2..
kelaklub
kelaklub
I am using the STLport and VC6, and these are the warnings I get when I encapsulate my Point class inside that namespace...


warning C4786: '_STL::stack<nm_geometric::Point<float>,_STL::deque<nm_geometric::Point<float>,_STL::allocator<nm_geometric::Point<float> > > >::~stack<nm_geometric::Point<float>,_STL::deque<nm_geometric::Point<float>,_S
TL::allocator<nm_geometric::Point<float> > > >' : identifier was truncated to '255' characters in the debug information
warning C4786: '_STL::_STLP_alloc_proxy<nm_geometric::Point<float> * *,nm_geometric::Point<float> *,_STL::allocator<nm_geometric::Point<float> > >::~_STLP_alloc_proxy<nm_geometric::Point<float> * *,nm_geometric::Point<do
uble> *,_STL::allocator<nm_geometric::Point<float> > >' : identifier was truncated to '255' characters in the debug information
Linking...

Run.exe - 0 error(s), 2 warning(s)


[edited by - kelaklub on January 4, 2004 3:21:54 PM]
kelaklub
kelaklub
I guess I can ignore these warnings, they only seem to happen in debug mode, release mode compiles without any hiccups. Thank you guys for your help.
Qw3r7yU10p!
Qw3r7yU10p!
1) the warning means exactly what it says in the documentation. It can be safely supressed using the pragma preprocessor directive as explained in the docs.

2) >> is taken by the compiler to mean bitshift operator

3) if you use a typedef it can make things clearer all round and avoid the bitshift confusion


typedef Point<float> FPoint;

typedef vector<FPoint> FPoints;

FPoint p1(1, 4, 0);
FPoint p2(7, 9, 0);
FPoint p3(4, 2, 0);
FPoints vertices;
vertices.push_back(p1);
vertices.push_back(p2);
vertices.push_back(p3);
Lektrix
Lektrix
I'm not sure if you have done this to enforce the type (badly might I add), but note that as this is a class template, the default value should not be 0.0, because this imposes a heavy requirement on the type you are instantiating with, tType. The default value that you probably want is tType():

template <typename tType>
class Point
{
public:
...

Point() : x( tType() ),
y( tType() ),
z( tType() ) {}
...
Point( const tType& X, const tType& Y, const tType& Z )
: x(X), y(Y), z(Z) {}

...
...
};

or

template <typename tType>
class Point
{
public:
...

Point( const tType& X = tType(), const tType& Y = tType(), const tType& Z = tType() )
: x(X), y(Y), z(Z) {}

...
...
};

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on January 4, 2004 5:41:02 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.