trouble with template comparision

This is a discussion on trouble with template comparision within the C++ Programming forums, part of the General Programming Boards category; I have a problem with this code. How can i call the compare function to compare 2 different types !!! ...

  1. #1
    Registered User actionbasti's Avatar
    Join Date
    Dec 2002
    Posts
    48

    trouble with template comparision

    I have a problem with this code. How can i call the compare function to compare 2 different types !!! It gives me an error message:

    Main.cpp
    (23): error C2664: 'Stack<int>::compare' : cannot convert parameter 1 from 'Stack<StackElement>' to 'Stack<StackElement>'
    with
    [
    StackElement=char
    ]
    and
    [
    StackElement=int
    ]




    Stack.h:
    ======


    Code:
    #ifndef STACK
    #define STACK
    
    const int STACK_CAPACITY=128;
    #include<assert.h>
    
    template<typename StackElement>
    class Stack
    {
    public:
       Stack();
       bool empty() const;
       void push(const StackElement&);
       void display() const;
       StackElement top() const;
       void pop();
       bool compare(Stack<StackElement> S1, Stack<StackElement> S2);
       //bool compare(Stack<StackElement> S1);
       friend ostream& operator <<(ostream&, const Stack<StackElement>&);
    private:
       StackElement myArray[STACK_CAPACITY];
       int myTop;
    };
    template<typename StackElement>
    Stack<StackElement>::Stack()
    {
    	myTop=-1;
    }
    
    template<typename StackElement>
    bool Stack<StackElement>::empty() const
    {
    	return (myTop==-1);
    }
    
    template<typename StackElement>
    void Stack<StackElement>::push( const StackElement& value)
    {
     	if (myTop<STACK_CAPACITY-1)
    	{
    		++myTop;
    		myArray[myTop]=value;
    	}
    	else
    	cerr<<"*** Stack is full---can't add new vale***\n";
    }
    template<typename StackElement>
    void Stack<StackElement>::display() const
    {
     	for (int i=myTop;i>=0;i--)
    	cout<<myArray[i]<<endl;
    }
    
    template<typename StackElement>
    StackElement Stack<StackElement>::top() const
    {
    	assert (!empty());
    	return myArray[myTop];
    }
    
    template<typename StackElement>
    void Stack<StackElement>::pop()
    {
    	if (myTop>=0)
    		myTop--;
    	else
    	cerr<<"*** Stack is empty--can't remove a value ***\n";
    }
    
    template<typename StackElement>
    ostream& operator<<(ostream& out, const Stack<StackElement> & st)
    {
    	for(int pos=st.myTop; pos>=0; pos--)
    		out<<st.myArray[pos]<<endl;
    	return out;
    }
    
    template<typename StackElement>
    bool Stack<StackElement>::compare(Stack<StackElement> S1, Stack<StackElement> S2)
    {
    
    	// code to compare the two array types
    
     	return 0;
    }
    
    #endif

    Main.cpp:
    =======

    Code:
    #include<iostream>
    using namespace std;
    #include"Stack.h"
    
    int main()
    {
    	Stack<int> s1;
    	Stack<char> s2;
    	
    	char str[]="ABCDEFG";
    	
    	for(int i=0;i<4;i++) s1.push(i);
    	
    	cout<<"Stack s1 contents\n";
    	s1.display();
    	cout<<"Stack s1 Top value: "<<s1.top();
    	
    	for(i=0;i<4;i++) s2.push(str[i]);
    	cout<<"\n\nStack s2 contents\n";
    	s2.display();
    	cout<<"Stack s2 Top value: "<<s2.top()<<endl<<endl;
    
    	s1.compare(s1,s2);
    
    	cout<<s1<<s2;
    
    
    	cout<<endl;
    
    	return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How can i call the compare function to compare 2 different types !!!
    At current, your template says that both types are the same:
    Code:
    template<typename StackElement>
    bool Stack<StackElement>::compare(Stack<StackElement> S1, Stack<StackElement> S2)
    If StackElement is int, both S1 and S2 are Stack<int> and C++'s type checking doesn't like it when you say you'll use int, but actually pass char. If you want two different types, add another template type:
    Code:
    template
    <
      typename StackElementA,
      typename StackElementB
    >
    bool Stack<StackElement>::compare(Stack<StackElementA> S1, Stack<StackElementB> S2)
    But please note that doing so isn't exactly the best idea. The error you've been getting is trying to tell you that mixing types is something to be avoided.
    My best code is written with the delete key.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,691
    Here is one way:
    Code:
    template<typename T1, typename T2>
    bool operator==(const Stack<T1> &s1, const Stack<T2> &s2)
    {
        // Compare s1 and s2
        return true;
    }
    Another possibility is to have a templated member function of the Stack<> class, but your compiler may not like it. VC++ 6.0 is ok with it as long as the implementation stays within the class declaration.
    Code:
    template<typename StackElement>
    class Stack
    {
    public:
        //...
        template<typename T2>
        bool compare(const Stack<T2> &s)
        {
            // Compare *this with s
            return true;
        }
        //...
    };
    The only issue with both of these is that you will not have access to private members of a passed in Stack<> class. This is because Stack<int> and Stack<char> are two physically different classes, and therefore one can't access the other's privates.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Template Trouble - Brainache Please Help
    By SpaceCadet in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2006, 09:51 AM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21