Dear all,
I was experimenting with a template taking two iterators for the range
of
a vector.(Perhaps, it is sth simple and I am missing it because it is
a
late hour.) I ran into problems in the compile phase , the code is
below:
#include <iostream>
#include <algorithm>
#include <stdexcept>
#include <vector>
using std::domain_error;
using std::sort;
using std::vector;
template <class T, class Ran>
T median(Ran b, Ran e)
{
typedef typename vector<T>::size_type vec_sz;
vec_sz size = (e-b)/sizeof(T);
if (size == 0)
throw domain_error("median of an empty vector");
sort(b, e);
vec_sz mid = size/2;
return size % 2 == 0 ? (b[mid] + b[mid-1]) / 2 : b[mid];
}
int main()
{
vector<doublevec;
for(int i=0;i!=10;++i)
vec.push_back(i);
std::cout << median(vec.begin(), vec.end()) << std::endl;
return 0;
}
I get
83.cc: In function 'int main()':
83.cc:38: error: no matching function for call to
'median(__gnu_cxx::__normal_iterator<double*, std::vector<double,
std::allocator<double >, __gnu_cxx::__normal_iterator<double*,
std::vector<double, std::allocator<double >)'
I am supplying the median function with iterators by using the begin
and
end member functions of the vector. However, I get a type mismatch
error
on iterators I guess. Could you clarify the problem for me?
Rgds,
--
Umut 7 1637
On May 13, 1:37 am, utab <umut.ta...@gmail.comwrote:
Dear all,
I was experimenting with a template taking two iterators for the range
of
a vector.(Perhaps, it is sth simple and I am missing it because it is
a
late hour.) I ran into problems in the compile phase , the code is
below:
#include <iostream>
#include <algorithm>
#include <stdexcept>
#include <vector>
using std::domain_error;
using std::sort;
using std::vector;
template <class T, class Ran>
T median(Ran b, Ran e)
{
typedef typename vector<T>::size_type vec_sz;
vec_sz size = (e-b)/sizeof(T);
if (size == 0)
throw domain_error("median of an empty vector");
sort(b, e);
vec_sz mid = size/2;
return size % 2 == 0 ? (b[mid] + b[mid-1]) / 2 : b[mid];
}
int main()
{
vector<doublevec;
for(int i=0;i!=10;++i)
vec.push_back(i);
std::cout << median(vec.begin(), vec.end()) << std::endl;
return 0;
}
I get
83.cc: In function 'int main()':
83.cc:38: error: no matching function for call to
'median(__gnu_cxx::__normal_iterator<double*, std::vector<double,
std::allocator<double >, __gnu_cxx::__normal_iterator<double*,
std::vector<double, std::allocator<double >)'
I am supplying the median function with iterators by using the begin
and
end member functions of the vector. However, I get a type mismatch
error
on iterators I guess. Could you clarify the problem for me?
Rgds,
--
Umut
Forgot to tell that I use g++ 4.1.2
On 13 May, 00:37, utab <umut.ta...@gmail.comwrote:
>
template <class T, class Ran>
T median(Ran b, Ran e)
{
* typedef typename vector<T>::size_type vec_sz;
* vec_sz size = (e-b)/sizeof(T);
* if (size == 0)
* * throw domain_error("median of an empty vector");
* sort(b, e);
* vec_sz mid = size/2;
* return size % 2 == 0 ? (b[mid] + b[mid-1]) / 2 : b[mid];
}
int main()
{
* vector<doublevec;
* for(int i=0;i!=10;++i)
* * vec.push_back(i);
* std::cout << median(vec.begin(), vec.end()) << std::endl;
The compiler cannot deduce T; replace the above call with
median<double>(vec.begin(), vec.end())
^^^^^^
* return 0;
}
On May 13, 1:47 am, tragomaskhalos <dave.du.verg...@logicacmg.com>
wrote:
On 13 May, 00:37, utab <umut.ta...@gmail.comwrote:
template <class T, class Ran>
T median(Ran b, Ran e)
{
typedef typename vector<T>::size_type vec_sz;
vec_sz size = (e-b)/sizeof(T);
if (size == 0)
throw domain_error("median of an empty vector");
sort(b, e);
vec_sz mid = size/2;
return size % 2 == 0 ? (b[mid] + b[mid-1]) / 2 : b[mid];
}
int main()
{
vector<doublevec;
for(int i=0;i!=10;++i)
vec.push_back(i);
std::cout << median(vec.begin(), vec.end()) << std::endl;
The compiler cannot deduce T; replace the above call with
median<double>(vec.begin(), vec.end())
^^^^^^
return 0;
}
Thanks, I read something similar in the faq as well.
On May 12, 4:37 pm, utab <umut.ta...@gmail.comwrote:
template <class T, class Ran>
T median(Ran b, Ran e)
{
According to that definition, there is no relation between T and Ran.
[...]
std::cout << median(vec.begin(), vec.end()) << std::endl;
The complier cannot deduce T for that call because the return values
are never a part of function signatures. You can try specifying it
yourself:
median<double>(vec.begin(), vec.end());
Ali
utab <um********@gmail.comwrote in news:50129083-18fd-4099-9ba7- 58**********@a23g2000hsc.googlegroups.com:
Dear all,
I was experimenting with a template taking two iterators for the range
of
a vector.(Perhaps, it is sth simple and I am missing it because it is
a
late hour.) I ran into problems in the compile phase , the code is
below:
#include <iostream>
#include <algorithm>
#include <stdexcept>
#include <vector>
using std::domain_error;
using std::sort;
using std::vector;
template <class T, class Ran>
T median(Ran b, Ran e)
{
You are passing in only Ran arguments, there is no way the compiler could
deduce the T template parameter.
typedef typename vector<T>::size_type vec_sz;
vec_sz size = (e-b)/sizeof(T);
if (size == 0)
throw domain_error("median of an empty vector");
sort(b, e);
vec_sz mid = size/2;
return size % 2 == 0 ? (b[mid] + b[mid-1]) / 2 : b[mid];
}
int main()
{
vector<doublevec;
for(int i=0;i!=10;++i)
vec.push_back(i);
std::cout << median(vec.begin(), vec.end()) << std::endl;
... so you have to specify it yourself:
std::cout << median<double>(vec.begin(), vec.end()) << std::endl;
... or throw away the redundant template parameter:
template <class Ran>
typename Ran::value_type median(Ran b, Ran e)
{
typedef typename vector<Ran::value_type>::size_type vec_sz;
vec_sz size = (e-b)/sizeof(Ran::value_type);
if (size == 0)
throw domain_error("median of an empty vector");
....
hth
Paavo
On May 12, 4:37*pm, utab <umut.ta...@gmail.comwrote:
I was experimenting with a template taking two iterators for the range
of
a vector.(Perhaps, it is sth simple and I am missing it because it is
a
late hour.) I ran into problems in the compile phase , the code is
below:
#include <iostream>
#include <algorithm>
#include <stdexcept>
#include <vector>
using std::domain_error;
using std::sort;
using std::vector;
template <class T, class Ran>
T median(Ran b, Ran e)
{
* typedef typename vector<T>::size_type vec_sz;
* vec_sz size = (e-b)/sizeof(T);
* if (size == 0)
* * throw domain_error("median of an empty vector");
* sort(b, e);
* vec_sz mid = size/2;
* return size % 2 == 0 ? (b[mid] + b[mid-1]) / 2 : b[mid];
}
The C++ compiler is not able to deduce the type "T" from the median()
function call. Since "T" is dependent on the iterator type "Ran", I
would eliminate "T" altogether. Note also that the calculation of
"size" is incorrect - e-b will return the number of positions between
two random access iterators.
Applying these suggestions produces a program like this one:
#include <iostream>
#include <algorithm>
#include <stdexcept>
#include <vector>
#include <iterator>
using std::domain_error;
using std::sort;
using std::vector;
template <class Ran>
typename std::iterator_traits<Ran>::value_type
median(Ran b, Ran e)
{
size_t size = e-b;
if (size == 0)
throw domain_error("median of an empty vector");
sort(b, e);
size_t mid = size/2;
return size % 2 == 0 ? (b[mid] + b[mid-1]) / 2 : b[mid];
}
int main()
{
vector<doublevec;
for (int i=0; i!=10; ++i)
vec.push_back(i);
std::cout << median(vec.begin(), vec.end()) << std::endl;
return 0;
}
Greg
On May 13, 1:57 am, Paavo Helde <nob...@ebi.eewrote:
utab <umut.ta...@gmail.comwrote in news:50129083-18fd-4099-9ba7-
586bb9df7...@a23g2000hsc.googlegroups.com:
I was experimenting with a template taking two iterators for
the range of a vector.(Perhaps, it is sth simple and I am
missing it because it is a late hour.) I ran into problems
in the compile phase , the code is below:
[...]
.. or throw away the redundant template parameter:
template <class Ran>
typename Ran::value_type median(Ran b, Ran e)
There's no guarantee that Ran has a typedef for value_type (and
there have definitely been implementations of the standard
library where it didn't for std::vector<>).
This should be:
template< typename RandomAccessIterator >
typename std::iterator_traits< RandomAccessIterator >::value_type
median( RandomAccessIterator begin, RandomAccessIterator end )
{
typedef typename vector<Ran::value_type>::size_type vec_sz;
And here, even worse, since you're tying yourself to
std::vector. (Even as it stands, you need an additional
typename in the template argument.) Also, the difference
between to random access iterators is a difference_type, not a
size_type. So it should be:
typedef typename std::iterator_traits< RandomAccessIterator >
::difference_type SequenceSize ;
vec_sz size = (e-b)/sizeof(Ran::value_type);
And of course, you don't want the division.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dave O'Hearn |
last post by:
This is a stripped down example that duplicates the problem I am
having. If I do it with concrete types, I can access the set's
iterator type correctly. If I make a template with generic types, I...
|
by: NKOBAYE027 |
last post by:
FIRST POST
Hi All: I'm trying to write a simple specialization before moving on to
something a bit more complex - always a good idea in my case, at least. :o)
I'm trying to adapt the example...
|
by: Ken Cecka |
last post by:
This is a contrived example to demonstrate a syntax problem I'm struggling
with:
#include <vector>
template <typename T>
class Class
{
};
|
by: darkstorm |
last post by:
Please check this program...When I compiles it in VC.net, it gives the
following error:
===============
Common\Lib\PList.h(115): error C2440: '=' : cannot convert from
'ListNode *' to...
|
by: red floyd |
last post by:
I'm playing with template template parameters. I'm not
sure what I'm doing wrong here. Could someone with better
template template experience give me a hand?
Code and Comeau online error...
|
by: Fab |
last post by:
All,
I need your help understanding why the following code does *NOT*
compile with G++ (tested with gcc 3.x and 4.1.x):
---------------------------------------------------------------------...
|
by: parvtb |
last post by:
I have the following code:
#include <list>
using namespace std;
template <typename T>
list<T>::iterator doStuff( list<T>::iterator myIter ) //--error here
|
by: anto.anish |
last post by:
Hi ,
Since, i did not want to write all instantiations in Source file of
all template methods for various different datatypes that my client
might use, Instead, i choose to write implementation...
|
by: anto.anish |
last post by:
Hi ,
Since, i did not want to write instantiations in Source file of all
template methods for various different datatypes that my client might
use, i choose to write implementation of template...
|
by: anto.anish |
last post by:
Hi ,
Since, i did not want to write explicit instantiations in Source file
of all template methods for various different datatypes that my client
might use, i choose to write implementation of...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
| |