Public Group

# templates, structs, typedefs & lists

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

## 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 on other sites
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 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;

##### Share on other sites

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

Hope this helps!

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

##### Share on other sites
JohnMMena - C++ does not have template typedefs.

##### Share on other sites
Quote:
 templatestruct point { T x, y; };templatetypedef list< point > 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 on other sites
Is my method unacceptable? It will work.... Just wondering if I'm way off in what I think you're aiming for..

##### 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 on other sites
what is the dif between pragma fury's suggestion, and
typedef list< point<int> > line;
?

##### Share on other sites
Quote:
 Original post by darkzeroxwhat is the dif between pragma fury's suggestion, andtypedef list< point > 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.

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633661
• Total Posts
3013220
×