Jump to content
  • Advertisement
Sign in to follow this  
darkzerox

templates, structs, typedefs & lists

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

all in one! how might i get this to compile?
template<class T>
struct point { T x, y; };
typedef list< point<?> > line;
first we create struct point, which will take ints, floats, doubles, or what have you, as long as they are the same, right..? then we want "line" to be a list of points, so long as each point has the same type. putting "int" in for the ? will compile, however, that's not what I want. replacing it with "T" also doesnt work. leaving the <?> out altogether doesnt work either.. so ya.. not really sure. also, just for clarification,
struct rect
{
       union { int x1, left; };
       union { int x2, right; };
       union { int y1, top; };
       union { int y2, bottom; };
};
will let me create a rect, and i can refer to left-most vertical line of the rectangle as "x1" or "left" and they are interchangable, correct? ie, if i change 'x1', it changes 'left'?

Share this post


Link to post
Share on other sites
Advertisement
You can't use "T" in your typedef, because it is outside of the template declaration, which ended at the end of your struct. If you don't want to use an actual type in your typedef, I'm not sure what you're trying to do..

Edit: I suppose you could do something like this:

template <class T>
struct point {
typedef std::list<point<T> > line;
T x, y;
};

typedef point<int> IntPoint;
IntPoint::line MyListOfIntPoints;




And yes, that struct of unions will work as you expect it to.

Share this post


Link to post
Share on other sites
Let me guess, you want to define the line type, and then be able to use the line type later on, with varous types for the points' members? Like:

line<int> IntLine;
line<float> FloatLine;
That would be considered a "typdef template", and is not supported in C++. Many people complain about this omission, and it might get added in the next version of C++, but for now, you'll have to live without it. If it was supported, though, it'd probably look like this:

template<class T>
struct point { T x, y; };

template<class T>
typedef list< point<T> > line;
Google for "Typedef Templates" for more information.

Share this post


Link to post
Share on other sites
Try this instead


template<class T>
struct point { T x, y; };

template<class T>
typedef list< point<T> > line;




Hope this helps!

[edit]
Doh! Agony, beat me to it! He is also correct on it not being supported by C++.
[/edit]

Share this post


Link to post
Share on other sites
Quote:
template<class T>
struct point { T x, y; };

template<class T>
typedef list< point<T> > line;


tried that before either of you suggested it, but thanks.
don't see why that shouldn't work... *shakes a fist at the C++ makers*

Share this post


Link to post
Share on other sites
Yeah, it's definitely a highly desired feature. I battled with this problem for a week when I was developing a console parser only to find out it wasn't my code, but the code in general.

Also, from what I've read on google, pragma Fury's example is the most common work around for this.

Happy coding!

Share this post


Link to post
Share on other sites
Quote:
Original post by darkzerox
what is the dif between pragma fury's suggestion, and
typedef list< point<int> > line;
?


None, really.. except that you define both the point type and the list to contain that point type with a single typedef. It also scopes the list to the type of point that it will contain, so it's nicely packaged.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!