templates, structs, typedefs & lists

Started by
26 comments, last by darkzerox 18 years, 10 months ago
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'?
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.
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.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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]
JohnMMena - C++ does not have template typedefs.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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*
Is my method unacceptable? It will work.... Just wondering if I'm way off in what I think you're aiming for..
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!
what is the dif between pragma fury's suggestion, and
typedef list< point<int> > line;
?
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.

This topic is closed to new replies.

Advertisement