2010-03-04 43 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