468,315 Members | 1,351 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,315 developers. It's quick & easy.

Problem with overloading operator *, I think

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::operator()
(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<TFoo<T>::operator*(int) [with
T = int] <near match>

Where am I going wrong?

- Eric

Feb 12 '07 #1
3 1344
Eric Lilja wrote:
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)
Make the line above

Foo operator*(const int val) const
{
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::operator()
(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<TFoo<T>::operator*(int) [with
T = int] <near match>

Where am I going wrong?
You were trying to call a non-const function for a const object.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 12 '07 #2
On 12 Feb 2007 12:11:31 -0800, "Eric Lilja" wrote:
>struct twice_over_functor
{
template<typename T>
bool operator()(const T& v1, const T& v2)
{
return (v2 == v1 * 2);
}
};
You probably need to write:

template<typename T>
struct twice_over_functor
{
bool operator()(const T& v1, const T& v2) const
{
return (v2 == v1 * 2);
}
};

Best wishes,
Roland Pibinger
Feb 12 '07 #3
On 12 Feb, 21:25, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Eric Lilja wrote:
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)

Make the line above

Foo operator*(const int val) const
{
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::operator()
(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<TFoo<T>::operator*(int) [with
T = int] <near match>
Where am I going wrong?

You were trying to call a non-const function for a const object.
Right, thanks for the quick reply! I was also missing an overload of
operator== as it turns out when I fixed that bug, but that was easily
resolved. Thanks again, Victor!

- Eric
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Feb 12 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by | last post: by
8 posts views Thread by Kelly Mandrake | last post: by
39 posts views Thread by zeus | last post: by
19 posts views Thread by Jess | last post: by
3 posts views Thread by kvnsmnsn | last post: by
2 posts views Thread by Colonel | last post: by
4 posts views Thread by =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post: by
9 posts views Thread by Faisal | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.