GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Mar-2006, 16:08
robynkartinian robynkartinian is offline
New Member
 
Join Date: Mar 2006
Posts: 9
robynkartinian is on a distinguished road
Exclamation

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:
vector<int> vect;

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  
Old 01-Mar-2006, 17:35
davis
 
Posts: n/a

Re: How do you make your own collections objects accept multiple types?


Quote:
Originally Posted by robynkartinian
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:
vector<int> vect;

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!

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:
template <typename T>
class YourClassName
{ ... };

...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  
Old 01-Mar-2006, 20:25
robynkartinian robynkartinian is offline
New Member
 
Join Date: Mar 2006
Posts: 9
robynkartinian is on a distinguished road

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  
Old 01-Mar-2006, 20:33
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

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:
//assuming:
template <typename T>
class Stuff {
    public:
        void doStuff(T this, char that);
};

//then:
template<typename T>
void Stuff<T>::doStuff(T this, char that)
{
...
}
HTH, check http://www.codersource.net/cpp_class_templates.html for more information
  #5  
Old 01-Mar-2006, 20:46
robynkartinian robynkartinian is offline
New Member
 
Join Date: Mar 2006
Posts: 9
robynkartinian is on a distinguished road

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:
template <typename T>
class BST {
  BSTNode<T>* root;
  int count;
  
  void Free( BSTNode<T>* ); // Used to delete tree
  void Init( const BST<T>& ); // Used for copy and assignment operator
  BSTNode<T>* InsertNode( BSTNode<T>* ); // Used to add a NODE to the tree
  BSTNode<T>* FindParent( BSTNode<T>* ); // Used to find the parent of a node
  BSTNode<T>* CopyTree( BSTNode<T>* ); // Used w/ Init()

 public:

  /*
    No-arg constructor.  Initializes an empty BST
  */
  BST();


  /*
    Copy constructor.  Makes a complete copy of its argument
  */
  BST(const BST<T> & other);

Code:
CPP / C++ / C Code:
template <typename T>
BST<T>::BST() : root(0), count(0) {
  // All variables initialized in initializer list
}
/*
  Copy constructor.  Makes a complete copy of its argument
*/
template <typename T>
BST<T>::BST(const BST<T> & other) : root(0), count(0) {
  Init( other );
}
/*
  Destructor
*/
template <typename T>
BST<T>::~BST() {
  Clear();
}

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  
Old 01-Mar-2006, 20:56
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: How do you make your own collections objects accept multiple types?


what errors is the linker giving you (copy and paste, please)?
  #7  
Old 01-Mar-2006, 20:58
robynkartinian robynkartinian is offline
New Member
 
Join Date: Mar 2006
Posts: 9
robynkartinian is on a distinguished road

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:
cd /home/brandon/WebCrawler/src/
make -C ../ test
make: Entering directory `/home/brandon/Computer Science/cs240/WebCrawler'
g++ -O -Wall -o bin/UnitTests obj/UnitTests.o obj/LinkedList.o obj/StopWords.o obj/Robot.o obj/URL.o obj/Page.o obj/PageBST.o obj/PageSet.o obj/BST.o -Lcs240utils/lib -lcs240utils
obj/StopWords.o: In function `StopWords::StopWords()':
StopWords.cpp:(.text+0xa): undefined reference to `BST<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::BST()'
obj/StopWords.o: In function `StopWords::StopWords()':
StopWords.cpp:(.text+0x1e): undefined reference to `BST<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::BST()'
obj/StopWords.o: In function `StopWords::Contains(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
StopWords.cpp:(.text+0x35): undefined reference to `BST<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Find(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const'
obj/StopWords.o: In function `StopWords::LoadFile(std::basic_istream<char, std::char_traits<char> >&)':
StopWords.cpp:(.text+0xe8): undefined reference to `BST<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Insert(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
obj/StopWords.o: In function `StopWords::Test(std::basic_ostream<char, std::char_traits<char> >&)':
StopWords.cpp:(.text+0xb69): undefined reference to `BST<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::~BST()'
StopWords.cpp:(.text+0xc3a): undefined reference to `BST<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::~BST()'
collect2: ld returned 1 exit status
make: *** [bin/UnitTests] Error 1
make: Leaving directory `/home/brandon/Computer Science/cs240/WebCrawler'

Compilation exited abnormally with code 2 at Wed Mar 1 19:49:58
  #8  
Old 01-Mar-2006, 21:11
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

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  
Old 01-Mar-2006, 21:15
robynkartinian robynkartinian is offline
New Member
 
Join Date: Mar 2006
Posts: 9
robynkartinian is on a distinguished road

Re: How do you make your own collections objects accept multiple types?


Yes, I instantiate a
CPP / C++ / C Code:
BST<string> stopWords;
as a member of the StopWords class. If you notice, in the error messages it calls the constructor and also generates an error.
  #10  
Old 01-Mar-2006, 21:27
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

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 GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 09:08.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.