#### Archived

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

# STL

This topic is 5308 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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
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
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
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
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
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
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
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
quote:

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

or even

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

1. 1
Rutin
24
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 46
• 41
• 23
• 13
• ### Forum Statistics

• Total Topics
631749
• Total Posts
3002040
×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!