Template newbie

Discussion in 'Mac Programming' started by dbrayford, Feb 7, 2011.

  1. dbrayford macrumors member

    Joined:
    Feb 22, 2010
    #1
    I get the following error message: "'void Calc(It,It,std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'T'"


    But as I am new to templates I don't know how to fix it.
    Code:
    void doSomething()
    {
    for(j = start; j < start + stride; j++)
         {
               stopsVec.push_back( allVec[j] );
         }
    
         Calc(stopsVec.begin(), stopsVec.end(), &out);
    }
    

    Header File

    Code:
    
    template<class It, class T>
    
    // iterator and value_type of *It
    
    void Calc(It begin, It end, std::pair<int, int> &out)
    {
    	std::vector<It>::iterator iter;
    	std::map<int, int> StartMap;
    	std::map<int, int>::reverse_iterator rit;
    
    	int sum, start, stop, count;
    	start = stop = 1;
    	count = sum = 0;
    
    	for(iter = begin; iter != end; iter++ )
    	{
    		sum += iter;
    		count++;
    		stop++;
    		if(sum <= 0)
    		{
    			// store original start
    			StartMap.insert(start, count);
    			// set new start position
    			start = stop;
    		}	
    	}
    
    	// set iterator to highest value
    	rit = StartMap.rbegin();
    
    	start = rit->first;
    	stop = start + rit->second;
    	
    	out.insert(start, stop);
    }
    
    
     
  2. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #2
    I assume that is not your header file in it's entirety? Anyway it doesn't look like you actually use T inside the header.
     
  3. dbrayford, Feb 7, 2011
    Last edited: Feb 7, 2011

    dbrayford thread starter macrumors member

    Joined:
    Feb 22, 2010
    #3
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    #include <vector>
    
    template<class It, class T>
    
    
    // iterator and value_type of *It
    void Calc(It begin, It end, std::pair<int, int> &out)
    {
        // begining of implementation:
        //...
    	
    	std::vector<It>::iterator iter;
    	std::map<int, int> StartMap;
    	std::map<int, int>::reverse_iterator rit;
    
    	int sum, start, stop, count;
    	start = stop = 1;
    	count = sum = 0;
    
    	for(iter = begin; iter != end; iter++ )
    	{
    		sum += iter;
    		count++;
    		stop++;
    		if(sum <= 0)
    		{
    			// store original start
    			StartMap.insert(start, count);
    			// set new start position
    			start = stop;
    		}	
    	}
    
    	// set iterator to highest value
    	rit = StartMap.rbegin();
    
    	start = rit->first;
    	stop = start + rit->second;
    	
    	out.first = start;
            out.second = stop;
    
        // end of implementation:
    }
    
    
    I just need to know how I call this templated function with vector iterators
    Normally I would just call
    Code:
    Calc(stopsVec.begin(), stopsVec.end(), &out);
    
    but don't ave a handle on calling templates and have tried this.
    Code:
    Calc<std::vector<int>::iterator, std::pair<int, int>>(*stopsVec.begin(), *stopsVec.end(), &out);
    
     
  4. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #4
    That looks completely odd to me, just a template declaration on it's own and a function. A template class, in it's simplest form.

    Code:
    template<T> class myThing {
    
            private:
                    T myVar;
    
            public:
                    void add(T amount){
                            myVar += amount;
                    }
    }
    
    
     
  5. dbrayford thread starter macrumors member

    Joined:
    Feb 22, 2010
    #5
    Fixed it, I've been suffering from the flu and haven't been thinking straight.
     

Share This Page