[test] User objects with BOOST_CHECK_EQUAL

Hi, I'm having a bit of trouble getting user-defined objects working with Boost.Test. I skimmed through the docs and I didn't see anything on this subject, but the boost documentation isn't the most intuitive documentation, so it is a bit easy to miss obvious stuff like this. Anyway, I have a class named "Foo", and I've given it an overloaded boolean == operator. When I do the following, it fails to compile under MSVC9: Foo a, b; BOOST_TEST_EQUAL( a, b ); The class is defined as follows: class Foo { private: std::string data; public: Foo() : data( "Testing 123" ) {} Foo( Foo const& other ) { data = other.data; } bool operator== ( Foo const& operand ) { return data == operand.data; } }; Below is the error I'm getting. How can I make this work? I've truncated it a little bit since it's insanely long: 1>c:\it\tfs\crusades\sdks\boost\boost\test\test_tools.hpp(342) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const Foo' (or there is no acceptable conversion) 1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(653): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup] 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(700): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup] 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(738): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup] 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ]

Robert Dailey wrote: [snip]
Anyway, I have a class named "Foo", and I've given it an overloaded boolean == operator. When I do the following, it fails to compile under MSVC9:
Dude, read the error: [snip]
1>c:\it\tfs\crusades\sdks\boost\boost\test\test_tools.hpp(342) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const Foo' (or there is no acceptable conversion)
No operator<< not ==. Boost test has something like this for CHECK_EQUAL(a,b): if(a!=b) { cout << "omg a!=b [a=" << a << ", b=" << b << "]" } -- Sohail Somani http://uint32t.blogspot.com

Yes, the streaming operator I ended up trying and it worked (after I posted), but one thing I left out in my original post was the fact that I had an overloaded casting operator to std::string, which I figured would fix the issue just as well as the stream operator would. I was wrong, and that is what motivated me originally to post. As far as this problem is concerned, however, it's solved. On Fri, May 2, 2008 at 3:36 PM, Sohail Somani <sohail@taggedtype.net> wrote:
Robert Dailey wrote: [snip]
Anyway, I have a class named "Foo", and I've given it an overloaded boolean == operator. When I do the following, it fails to compile under MSVC9:
Dude, read the error:
[snip]
1>c:\it\tfs\crusades\sdks\boost\boost\test\test_tools.hpp(342) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const Foo' (or there is no acceptable conversion)
No operator<< not ==. Boost test has something like this for CHECK_EQUAL(a,b):
if(a!=b) { cout << "omg a!=b [a=" << a << ", b=" << b << "]" }
-- Sohail Somani http://uint32t.blogspot.com _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

I believe template resolution rules prohibit implicit conversion. You need to define operator<<, or use BOOST_TEST_DONT_PRINT_LOG_VALUE( Foo ); Gennadiy "Robert Dailey" <rcdailey@gmail.com> ???????/???????? ? ???????? ?????????: news:496954360805021435p6cbc5c60oadae02d7de234d96@mail.gmail.com... Yes, the streaming operator I ended up trying and it worked (after I posted), but one thing I left out in my original post was the fact that I had an overloaded casting operator to std::string, which I figured would fix the issue just as well as the stream operator would. I was wrong, and that is what motivated me originally to post. As far as this problem is concerned, however, it's solved. On Fri, May 2, 2008 at 3:36 PM, Sohail Somani <sohail@taggedtype.net> wrote: Robert Dailey wrote: [snip] > Anyway, I have a class named "Foo", and I've given it an overloaded > boolean == operator. When I do the following, it fails to compile under > MSVC9: Dude, read the error: [snip] > 1>c:\it\tfs\crusades\sdks\boost\boost\test\test_tools.hpp(342) : error > C2679: binary '<<' : no operator found which takes a right-hand operand > of type 'const Foo' (or there is no acceptable conversion) No operator<< not ==. Boost test has something like this for CHECK_EQUAL(a,b): if(a!=b) { cout << "omg a!=b [a=" << a << ", b=" << b << "]" } -- Sohail Somani http://uint32t.blogspot.com _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users ------------------------------------------------------------------------------ _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Fri, May 02, 2008 at 02:54:19PM -0500, Robert Dailey wrote:
I'm having a bit of trouble getting user-defined objects working with Boost.Test. I skimmed through the docs and I didn't see anything on this subject, but the boost documentation isn't the most intuitive documentation, so it is a bit easy to miss obvious stuff like this.
Hopefully you read the current docs, the one on www.boost.org is outdated but not yet replaced.
Anyway, I have a class named "Foo", and I've given it an overloaded boolean == operator. When I do the following, it fails to compile under MSVC9:
The class is defined as follows:
class Foo { bool operator== ( Foo const& operand )
You should make this operator a const one. This isn't your problem but ... bool operator== ( Foo const& operand ) const
{ return data == operand.data; } };
Jens
participants (4)
-
Gennadiy Rozental
-
Jens Seidel
-
Robert Dailey
-
Sohail Somani