2012-10-06 5 views
2

전체 코드를 실행해야하는 시간을 표시하려면 부스트 auto_cpu_timer을 사용하고 싶습니다. 또한 progress_display을 사용하여 루프의 진행 상황을보고 싶습니다.boost auto_cpu_timer와 progress_display를 동시에 사용하는 방법은 무엇입니까?

부스트는 두 개의 타이머 클래스를 가지고 있기 때문에 네임 스페이스에 문제가있는 것 같습니다. 여기서 progress_display은 더 이상 사용되지 않는 라이브러리입니다.

http://www.boost.org/doc/libs/1_51_0/libs/timer/doc/index.html

그럼에도 불구하고,이를 달성하는 방법은 무엇입니까? 다음 예제는 내가하려고하는 것을 보여줍니다. AUTO 또는 PROG을 사용하면 문제가 없지만 둘 다 함께 오류 메시지가 표시됩니다.

홈페이지 :g++ -lboost_timer main.cc -o time

#define AUTO 
#define PROG 

#ifdef PROG 
#include <boost/progress.hpp> 
#endif  //---- PROG ----- 

#ifdef AUTO 
#include <boost/timer/timer.hpp> 
#endif  //---- AUTO ----- 

#include <cmath> 

int main() 
{ 
#ifdef AUTO 
    boost::timer::auto_cpu_timer t; 
#endif  //---- AUTO ----- 

    long loops = 100000000; 

#ifdef PROG 
    boost::progress_display pd(loops); 
#endif  //---- PROG ----- 

    //long loop to burn some time 
    for (long i = 0; i < loops; ++i) 
    { 
     std::sqrt(123.456L); 
#ifdef PROG 
     ++pd; 
#endif  //---- PROG ----- 
    } 

    return 0; 
} 

오류 로그 컴파일 : 당신이 boost/progress.hpp 포함하면

/usr/include/boost/timer/timer.hpp:38:1: error: ‘namespace boost::timer { }’ redeclared as different kind of symbol 
/usr/include/boost/timer.hpp:45:1: error: previous declaration of ‘class boost::timer’ 
/usr/include/boost/timer/timer.hpp: In member function ‘std::string boost::timer::cpu_timer::format(short int, const std::string&) const’: 
/usr/include/boost/timer/timer.hpp:74:34: error: ‘format’ is not a member of ‘boost::timer’ 
/usr/include/boost/timer/timer.hpp: In member function ‘std::string boost::timer::cpu_timer::format(short int) const’: 
/usr/include/boost/timer/timer.hpp:76:34: error: ‘format’ is not a member of ‘boost::timer’ 
main.cc: In function ‘int main()’: 
main.cc:17:2: error: ‘auto_cpu_timer’ is not a member of ‘boost::timer’ 
main.cc:17:31: error: expected ‘;’ before ‘t’ 

답변

6

는 C는 ++ 컴파일러입니다 boost/timer.hpp에 정의 class timerboost::timer에 대한 정의를 본다 boost/progress.hpp에 포함되어 있습니다.

boost/time/timer.hpp을 포함하면 C++ 컴파일러에서 boost::timer에 대한 다른 정의가 네임 스페이스로 표시되며 이것이 오류의 원인입니다.

정말로 사용하고 싶다면 해결책은 매크로를 통해 boost::timer 중 하나의 이름을 바꾸는 것입니다. 그러나 namespace boost::timer에는 std::string format(const cpu_times& times, short places, const std::string& format)과 같은 헤더 외부에서 구현되는 함수가 포함되어 있으므로 class boost::timer의 이름을 바꾸어야합니다. 따라서 코드는 다음과 같습니다.

#ifdef PROG 
#define timer timer_class 
#include <boost/progress.hpp> 
#undef timer 
#endif  //---- PROG ----- 

#ifdef AUTO 
#include <boost/timer/timer.hpp> 
#endif  //---- AUTO ----- 
+0

우수! –

+0

문제는 여전히 1.58에 있습니다. 부스트가 의도 한 동작입니까? – stfn

+1

나는 잘 모르겠다. 부스트 녀석에게 무엇이 문제인지 물어봐야한다. – BigBoss

관련 문제