E
Eric Lilja
Hello group! I tried to make a simple program to exhibit why making a
operator() in a functor a member template is a good idea, but I can't
get this code to compile, hehe. I've reduced it to the following
complete test program:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
class Foo
{
public:
explicit Foo(const T& v) : m_v(v) {}
bool operator==(const Foo& rhs) // Should I actually write const
Foo<T>& rhs here, btw?
{
return m_v == rhs.m_v;
}
Foo operator*(const int val)
{
T v = m_v * val;
return Foo(v);
}
private:
T m_v;
};
struct twice_over_functor
{
template<typename T>
bool operator()(const T& v1, const T& v2)
{
return (v2 == v1 * 2);
}
};
typedef vector<Foo<int > >::const_iterator constitr;
int
main()
{
vector<Foo<int> > v;
int n;
while (cin >> n)
{
v.push_back(Foo<int>(n));
}
constitr itr = adjacent_find(v.begin(), v.end(),
twice_over_functor());
if (itr != v.end())
{
cout << "Pair found!" << endl;
}
else
{
cout << "No pairs found!" << endl;
}
}
The error I get when compiling is:
$ g++ -Wall -Wextra -std=c++98 -pedantic -g copy.cpp -o runme
copy.cpp: In member function `bool twice_over_functor:perator()
(const T&, const T&) [with T = Foo<int>]':
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:395:
instantiated from `_ForwardIterator
std::adjacent_find(_ForwardIterator, _Forwa
rdIterator, _BinaryPredicate) [with _ForwardIterator =
__gnu_cxx::__normal_iterator<Foo<int>*, std::vector<Foo<int>,
std::allocator<Foo<int> > > >,
_BinaryPredicate = twice_over_functor]'
copy.cpp:51: instantiated from here
copy.cpp:34: error: no match for 'operator*' in 'v1 * 2'
copy.cpp:19: note: candidates are: Foo<T> Foo<T>:perator*(int) [with
T = int] <near match>
Where am I going wrong?
- Eric
operator() in a functor a member template is a good idea, but I can't
get this code to compile, hehe. I've reduced it to the following
complete test program:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
class Foo
{
public:
explicit Foo(const T& v) : m_v(v) {}
bool operator==(const Foo& rhs) // Should I actually write const
Foo<T>& rhs here, btw?
{
return m_v == rhs.m_v;
}
Foo operator*(const int val)
{
T v = m_v * val;
return Foo(v);
}
private:
T m_v;
};
struct twice_over_functor
{
template<typename T>
bool operator()(const T& v1, const T& v2)
{
return (v2 == v1 * 2);
}
};
typedef vector<Foo<int > >::const_iterator constitr;
int
main()
{
vector<Foo<int> > v;
int n;
while (cin >> n)
{
v.push_back(Foo<int>(n));
}
constitr itr = adjacent_find(v.begin(), v.end(),
twice_over_functor());
if (itr != v.end())
{
cout << "Pair found!" << endl;
}
else
{
cout << "No pairs found!" << endl;
}
}
The error I get when compiling is:
$ g++ -Wall -Wextra -std=c++98 -pedantic -g copy.cpp -o runme
copy.cpp: In member function `bool twice_over_functor:perator()
(const T&, const T&) [with T = Foo<int>]':
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:395:
instantiated from `_ForwardIterator
std::adjacent_find(_ForwardIterator, _Forwa
rdIterator, _BinaryPredicate) [with _ForwardIterator =
__gnu_cxx::__normal_iterator<Foo<int>*, std::vector<Foo<int>,
std::allocator<Foo<int> > > >,
_BinaryPredicate = twice_over_functor]'
copy.cpp:51: instantiated from here
copy.cpp:34: error: no match for 'operator*' in 'v1 * 2'
copy.cpp:19: note: candidates are: Foo<T> Foo<T>:perator*(int) [with
T = int] <near match>
Where am I going wrong?
- Eric