![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
How do you make your own collections objects accept multiple types?I am trying to write a Binary Search Tree that will accept and store different types (aka strings, ints, classes, etc). I know that in the Standard Template Library you would use something like:
Code:
I want to be able to do this with my own classes. If anyone knows the answer and/or can refer me to a website that has the answer, that'd be great, thanks! |
|
#2
|
|||
|
|||
Re: How do you make your own collections objects accept multiple types?Quote:
If I'm understanding what you mean properly, you can create a compile-time generic container class using templates, but the type must be specified at compilation time for each instance of it, but each instance may contain a different type. If you want a truly heterogenous type container, it is much more difficult because you would basically need to implement a new type system whereby all of your types are derived from the same type ala "Mother-of-all-Objects" such as Objective C and Java, and to a lesser extent Qt and MFC. CPP / C++ / C Code:
...you simply need to implement the class based on your needs. However, you would most likely find that you would want to conform to an STL container concept so that you can use standard algorithms. Check out: www.sgi.com ...for more details. :davis: |
|
#3
|
|||
|
|||
Re: How do you make your own collections objects accept multiple types?OK, so I figured out how to make my class use templates, and it compiles, but for some reason when I link w/ another file that uses that class, it gives me a whole bunch of "undefined reference to (function or constructor)" errors. Any idea what my problem might be?
|
|
#4
|
|||
|
|||
Re: How do you make your own collections objects accept multiple types?well, did you implement the class's functions? declaring members of a template class can be tricky:
CPP / C++ / C Code:
|
|
#5
|
|||
|
|||
Re: How do you make your own collections objects accept multiple types?As far as I can tell, that's exactly how I implemented my class. Here is a sample:
Header: CPP / C++ / C Code:
Code: CPP / C++ / C Code:
I realize this is a small sample and perhaps not very readable. It's part of a Binary Search Tree. I had to implement both the BST class and the BSTNode class to use templates. Perhaps this is the problem? I'm open to suggestions |
|
#6
|
|||
|
|||
Re: How do you make your own collections objects accept multiple types?what errors is the linker giving you (copy and paste, please)?
|
|
#7
|
|||
|
|||
Re: How do you make your own collections objects accept multiple types?This is my error. It's part of a bigger project, so I wouldn't be surprised if the error just lies elsewhere
Quote:
|
|
#8
|
|||
|
|||
Re: How do you make your own collections objects accept multiple types?Um. Apparently the linker doesn't know what to do with a BST<std::string>. What is this StopWords? Does it try to use BST<string> without instantiating one? Someone else is going to know more about this than I, but I don't think code is actually generated for templates for a specific type unless it is instantiated, usually by declaring an object of the template with that type. In other words, if you don't use a BST<string> anywhere, the linker might not be finding ~BST<string>() because the compiler didn't bother to generate code for it. Not really sure how to fix this. Someone want to bail me out, because I'm kind of jumping in over my head here?
|
|
#9
|
|||
|
|||
Re: How do you make your own collections objects accept multiple types?Yes, I instantiate a
CPP / C++ / C Code:
|
|
#10
|
|||
|
|||
Re: How do you make your own collections objects accept multiple types?weird...the only thing I can think of now is that you are not compiling with the .cpp file in which you define the BST<> members.
|
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| multiple types in one declaration ?! | weeb0 | C++ Forum | 2 | 25-Jan-2006 21:23 |
| Passing multiple objects in functions | crystalattice | C++ Forum | 3 | 08-Oct-2004 09:18 |
| controling dialog objects & multiple source files | omills | MS Visual C++ / MFC Forum | 0 | 15-Jul-2004 00:30 |
| IP tables | rogermark100 | C Programming Language | 6 | 18-Apr-2004 08:22 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The