Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dbrayford

macrumors member
Original poster
Feb 22, 2010
41
0
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);
}
 
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.
 
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);
 
Last edited:
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;
                }
}
 
Fixed it, I've been suffering from the flu and haven't been thinking straight.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.