Problem with overloading operator *, I think

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

Where am I going wrong?

- Eric
 
V

Victor Bazarov

Eric said:
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::eek: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>::eek:perator*(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
 
R

Roland Pibinger

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
 
E

Eric Lilja

Eric said:
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::eek: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>::eek:perator*(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top