?

Log in

No account? Create an account
Failure. - Chaz Meyers [entries|archive|friends|userinfo]
Chaz Meyers

[ website | chazmeyers.com ]
[ profile | view profile ]
[ archive | view archive ]

Links
[Links:| chazmeyers.com Twitter ]

Failure. [Nov. 6th, 2002|11:03 am]
Chaz Meyers
I got started on #1, and finished writing a binary tree library. Then I decided to get fancy and write it up as a template.

Then, I remembered I don't know how to do that, so I have to go back and fix it. Can structs even be templates? It should. structs are just classes that are public by default. I don't think you can do something along the lines of typedef btnode<T>* btpointer<T>, unfortunately.

Maybe because my functions aren't members. *sigh*

I suppose I can either strip out all of the template junk and do the assignment as described, or I can make a binary tree class template.

I know what I should do: I should strip out the template junk, get the intcollection working (int collection is a client to this library) and then do 72, and then, if I have time, make a binary tree class template. Whether or not I'll do that, we'll see.

This has to be pretty sad. I've been working on this since I got out of class at 3:30, with a half hour break for dinner. Not that I'm particularly attached to doing this lab. If anyone called me and gave me a horrible excuse not to work on this, I would've been out of here. Where is everyone, anyway?

Hm. Well, I need to get out of this room, and if no one's going to make an excuse for me to leave, I'll make one of my own. A walk in the cold brisk air wearing my borrowed warm kickass winter jacket will be good for the body, mind, and soul. When I get back, I'm going to get the intcollection working and admit defeat for now. Maybe. I can be stuborn at times.

Is it only Wednesday? Going home threw off my internal calendar. Got lots of great music the one night I was home.. must bring CD's with me next time to burn all of it.
LinkReply

Comments:
From: catch2002
2002-11-11 04:09 pm (UTC)

fun c++ esoteric crap

"Then, I remembered I don't know how to do that, so I have to go back and fix it. Can structs even be templates? It should. structs are just classes that are public by default. I don't think you can do something along the lines of typedef btnode* btpointer, unfortunately."

ok, I think I gather what you are driving at and yes I believe it is possible. I had to do a program last year where we simulated the huffman coding algorithm and it used trees and crap like that. and one data structure combo that was especially deadly was this:

struct HuffmanPair
{
int occurences;
string AsciiCharacter;
};

templte
struct BinaryTreeNode
{
Item data;
BinaryTreeNode* left;
BinaryTreeNode* right;
};


so basically what this does is: the template is going to be a node with stores three things in it. imagine that in the middle is the "data" of type Item (any type you want) in the left is the left pointer and in the right is the right pointer. Now the "data" is going to be type HuffmanPair for my program, which is a struct of the letter and how many times that letter occurs in a word. however, the "data" could be of type int, string, or whatever makes sense. but if can also be a homemade type to make things easier to manipulate. hope this helps
(Reply) (Thread)
[User Picture]From: cpm
2002-11-11 08:18 pm (UTC)

Re: fun c++ esoteric crap

Thanks. :) It's nice to know that my guess that structs and classes can both work with templates. It seems as though LJ's HTML stripper removed everything in <'s and >'s, but I get the gist of it anyway. :)

This is my one big stumbling block, which from my Google searches lead me to believe the following is not allowed in C++:
My professor likes typedefing pointers in our binary trees, linked lists, etc, like so:
typedef BinaryTreeNode* BinaryTreeNodePtr;

Any thoughts on how such a typedef could work if the BinaryTreeNode was a template, as in your example above? I tried playing around with some different syntaxes, which my compiler all hated. :)
My first guess would be something along the lines of:
typedef BinaryTreeNode* BinaryTreeNodePtr

I think google searches lead me to something saying it basically wasn't allowed because it would make the language really complex that worked. I may have misread it, though. :)

I imagine this could be achieved with a preprocessor parameterized macro, but that sounds very ugly to me. In C that would be fine, but in C++ I would find it very hard to justify such a hack.
(Reply) (Parent) (Thread)
[User Picture]From: cpm
2002-11-11 08:19 pm (UTC)

Re: fun c++ esoteric crap

Hah. Second code segment should read:
typedef BinaryTreeNode* <T> BinaryTreeNodePtr <T>
(Reply) (Parent) (Thread)