Thread: Template problem

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    33

    Template problem

    I'm back

    Here is my Set class:

    Code:
    template <class T>
    class Set{
    	private:
    		T *arr;
    		int size;
    		int num;
    	public:
    		void insert(T);
    		bool contains(T);
    		void print(void);
    		void sort(void);
    		Set findIntersect(Set<T>*);
    		//Set findUnion(Set*);
    		Set(int);
    		~Set();
    };
    I am trying to implement the findIntersect function (and findUnion for that matter) which takes a Set object created by:

    Code:
    Set<int> *s2 = new Set<int>(5);
    In the main file, and compares THIS set to the one in given as an argument.

    The problem is, I am getting an error and probably is a small syntax issue (first time working with templates)

    Code:
    template <class T>
    Set Set<T>::findIntersect(Set<T> *s2){
    	return s2;
    }
    As you can see ATM the function does nothing, but I am just trying to get it working first.

    If I change the return type from Set to void, and just print something, it works, so I am guessing it has to do with that.

    Error(s) given by VS2008:
    Error 1 error C2955: 'Set' : use of class template requires template argument list

    Error 2 error C2244: 'Set<T>::findIntersect' : unable to match function definition to an existing declaration

    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What kind of Set do you want to return? You should return a Set<T>.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The error is simple: s2 is a Set pointer, but you want to return a Set object.

    I would propose that findIntersect be a member function with this signature:
    Code:
    Set<T> findIntersect(const Set<T>&) const;
    or a free function with this signature:
    Code:
    Set<T> findIntersect(const Set<T>&, const Set<T>&);
    The problem is that it appears that your internal array is not necessarily sorted (otherwise the sort member function would be redundant), so contains() cannot do binary search, and you actually need to sort the internal arrays in order to implement a fast algorithm for findIntersect, but sort clearly should be a non-const member function. On the other hand, although you can directly sort the internal array if findIntersect is a member function, this is a violation of const-correctness (the observable, logical, state of the object changes), even though it probably will not be detected by the compiler.

    Note that std::set is typically implemented using a balanced binary tree.

    EDIT:
    Oh yes, but tabstop's observation is more pertinent.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    33
    I wanted to return my definition of a Set

    I did try Set<T> before I posted this, and it gives me 1 error:

    error C2664: 'Set<T>::Set(int)' : cannot convert parameter 1 from 'Set<T> *' to 'int'

    *To laserlight*

    I just wrote a bubble sort and call it at the end of my insert because I didn't want to actually insert them in order, so technically it is sorted

    And just for clarification, I am referring to set theory ideas of intersections and unions.
    Last edited by chinesepirate; 11-06-2008 at 09:49 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, that's an error from the constructor, not this function. You're apparently trying to pass a pointer to a Set into your constructor, but your constructor only takes an integer. Since I can't see any relevant code, I have no idea what you wanted to do here.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    33
    Eh I figured it out, just changed return type to Set<T>*

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by chinesepirate
    Eh I figured it out, just changed return type to Set<T>*
    Why do you want to use a pointer to a Set<T> as a parameter and then return a pointer to a Set<T>? In fact, what are you going to return? You cannot return a pointer to a local variable, so you will have to return a pointer to a dynamically allocated Set<T>, but that means that the caller has to manage memory, thus defeating one of the advantages of RAII with objects. Also, does your code handle a null pointer argument correctly?

    If you pass by const reference as in my suggested function signatures, and then return by value, you will not have these problems. You may want to change the return by value into an out parameter (e.g., one that takes the resulting Set<T> by reference) to avoid extra copying, but this may not even matter at all due to named return value optimisation.
    Last edited by laserlight; 11-06-2008 at 10:27 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I just wrote a bubble sort
    There's another error ...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by laserlight View Post
    If you pass by const reference as in my suggested function signatures, and then return by value, you will not have these problems. You may want to change the return by value into an out parameter (e.g., one that takes the resulting Set<T> by reference) to avoid extra copying, but this may not even matter at all due to named return value optimisation.
    And also, consider a completely different interface for these operations. What about an "intersection iterator" and "union iterator" concept which when constructed from two sets produces an iterator which iterates, correspondingly, over the items only in both sets, or in items from either set?

    Typically the "real work" of using any container is iterating over it, so conceptualizing set operations in terms of iterators instead of operations on sets might be useful -- it would also cut down on copying and memory usage.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template object creation problem
    By babu198649 in forum C++ Programming
    Replies: 7
    Last Post: 09-16-2008, 04:02 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  5. Problem with template usage
    By rmullen3 in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2002, 06:30 PM