2010-03-04 70 views
2

我测试C++类具有多项功能,所有具有基本相同的形式:内部编译器错误和boost ::绑定

ClassUnderTest t; 

DATATYPE data = { 0 }; 
try 
{ 
    t.SomeFunction(&data); 
} 
catch(const SomeException& e) 
{ 
    // log known error 
} 
catch(...) 
{ 
    // log unknown error 
} 

由于有很多这样的,我想我会写一个函数来做到最繁重的工作:

template< typename DATA, typename TestFunction > 
int DoTest(TestFunction test_fcn) 
{ 
    DATA data = { 0 }; 
    try 
    { 
     test_fcn(&data); 
    } 
    catch(const SomeException& e) 
    { 
     // log known error 
     return FAIL; 
    } 
    catch(...) 
    { 
     // log unknown error 
     return FAIL; 
    } 
    return TRUE; 
} 

ClassUnderTest t; 
DoTest<DATATYPE>(boost::bind(&ClassUnderTest::SomeFunction, boost::ref(t))); 

但是,编译器似乎并不同意我认为这是个好主意......

Warning 1 warning C4180: qualifier applied to function type has no meaning; ignored c:\boost\boost_1_41_0\boost\bind\bind.hpp 1657 
Warning 2 warning C4180: qualifier applied to function type has no meaning; ignored c:\boost\boost_1_41_0\boost\bind\mem_fn.hpp 318 
Warning 3 warning C4180: qualifier applied to function type has no meaning; ignored c:\boost\boost_1_41_0\boost\bind\mem_fn.hpp 326 
Warning 4 warning C4180: qualifier applied to function type has no meaning; ignored c:\boost\boost_1_41_0\boost\bind\mem_fn.hpp 331 
Warning 5 warning C4180: qualifier applied to function type has no meaning; ignored c:\boost\boost_1_41_0\boost\bind\mem_fn.hpp 345 
Warning 6 warning C4180: qualifier applied to function type has no meaning; ignored c:\boost\boost_1_41_0\boost\bind\mem_fn.hpp 350 
Warning 7 warning C4180: qualifier applied to function type has no meaning; ignored c:\boost\boost_1_41_0\boost\bind\mem_fn.hpp 362 
Error 8 fatal error C1001: An internal error has occurred in the compiler. c:\boost\boost_1_41_0\boost\bind\mem_fn.hpp 328 

我正在使用Visual Studio 2008 SP1。 如果有人能指出我做错了什么,我将不胜感激。

感谢, PaulH

回答

7

的错误是在你的代码,而不是在bind。你传递一个不期望任何参数的函子。相反,你的电话的,做

DoTest<DATATYPE>(boost::bind(&ClassUnderTest::SomeFunction, &t, _1)); 

如果省略_1然后bind将创建一个零参数函数对象和成员函数(这需要一个数据指针)将错过一个参数时bind调用。

+0

我可以忽略这些警告,但我希望无论我如何解决警告,都会产生不会导致编译器崩溃的代码。 – PaulH 2010-03-04 16:45:10

+0

那么,有没有办法让这个有效的C++ 03? – PaulH 2010-03-04 16:49:02

+1

@PaulH,我不太了解'bind'来说明它是如何在内部工作的,并且不足以说明为什么它会崩溃。对不起。也许另一个人可以在这里更多地了解这个问题。也许尝试传递'&t'而不是'ref(t)'或者尝试传递'boost :: mem_fn(&Class :: Function)'来直接绑定第一个参数而不是'&Class :: Function'。 – 2010-03-04 16:55:45