• ### Announcements

#### Archived

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

# STL

## Recommended Posts

kelaklub    160
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]

##### Share on other sites
std::stack< Point<float> >   myStack;                        ^                        |                        |                        +--- do not forget this space

##### Share on other sites
jdhardy    469
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]

##### Share on other sites
kelaklub    160
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.

##### Share on other sites
Nervo    344
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]

##### Share on other sites
kelaklub    160
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>,_STL::allocator<nm_geometric::Point<float> > > >' : identifier was truncated to '255' characters in the debug informationwarning 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<double> *,_STL::allocator<nm_geometric::Point<float> > >' : identifier was truncated to '255' characters in the debug informationLinking...Run.exe - 0 error(s), 2 warning(s)

[edited by - kelaklub on January 4, 2004 3:21:54 PM]

##### Share on other sites
kelaklub    160
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.

##### Share on other sites
petewood    819
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);

##### Share on other sites
Lektrix    106
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]

##### Share on other sites
Guest Anonymous Poster
quote:

Point() : x( tType() ),
y( tType() ),
z( tType() ) {}

or even

Point() : x(), y(), z() {}

##### Share on other sites
Guest Anonymous Poster
quote:

Also just Point() {}

Alas, this will not call the default constructors for x,y or z unless Point is static.

And possibly one or two other situations but I would have to look those up :D

*rummages through notes*

Undefined elements of an array, for example int a[10]={1};

int *a1=new int
int *a2=new int()

should also differ, I believe, in the same way (but appear not to on g++) with *a1 = any value and *a2 = 0

It''s a bloody minefield and has caused several bugs ... one of which I found today

quote:

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

Should cope nicely!

##### Share on other sites
Guest Anonymous Poster
quote:

Alas, this will not call the default constructors for x,y or z unless Point is static

"Initialize to 0" instead of "default constructor" for built in types.