2012-09-28 2 views
0

이 COMPARE 매크로가 작동하는 데 문제가 있습니다. 수정 방법에 대한 아이디어가 있습니까?전 처리기 문자열 매크로가 작동하지 않습니다.

약간의 인공 샘플 - 나는 가능한 한 작게 만들고 싶었습니다.

#include <iostream> 
#include <string.h> 

enum rqtypes {Unknown, Monitor, Query, Snapshot }; 

class base { 
public: 
    base() : type(Unknown) {} 
    rqtypes type; 
}; 

class CBMonitorDeviceRequest : public base 
{ 
public: 
    CBMonitorDeviceRequest() : dn(0) {} 
    char* dn; 
}; 


//I want the equivalent of: 
//  case MonitorDeviceRequestID: 
//   CBMonitorDeviceRequest* pthis = static_cast<CBMonitorDeviceRequest*>(thisrq); 
//   if(pthis && strcmp(pthis->dn1, "1234") == 0) 
//    return 0; 
//   else 
//    return -1; 
//   break; 


int main(int argc, char* argv[]) 
{ 
    CBMonitorDeviceRequest* ptr = new CBMonitorDeviceRequest; 
    ptr->type = Monitor; 
    ptr->dn = new char(strlen("1234") + 1); 
    strcpy(ptr->dn, "1234"); 

#define COMPARE(id, thismsg) case id##ID: { \ 
    CB##id * pthis = static_cast<CB##id *>(thismsg); \ 
    if(pthis && strcmp(pthis->dn, "1234") == 0) \ 
     std::cout << "found"; \ 
    else \ 
     std::cout << "not found"; \ 
    break; } \ 

    switch(ptr->type){ 
     COMPARE(Monitor, ptr); 
    } 

#undef COMPARE 

    return 0; 
} 

내가 얻을 예 :

(46) : error C2065: 'MonitorID' : undeclared identifier 
(46) : error C2051: case expression not constant 
(46) : error C2065: 'CBMonitor' : undeclared identifier 
(46) : error C2065: 'pthis' : undeclared identifier 
(46) : error C2061: syntax error : identifier 'CBMonitor' 
(46) : error C2065: 'pthis' : undeclared identifier 
(46) : error C2065: 'pthis' : undeclared identifier 
(46) : error C2227: left of '->dn' must point to class/struct/union/generic type 

얻을 -ei GCC를 사용 : # 30 "macro_fun2.cpp" INT 주 (INT의는 argc를 숯불 * 변수는 argv []) { CBMonitorDeviceRequest * ptr = 새로운 CBMonitorDeviceRequest; ptr-> type = Monitor; ptr-> dn = new char (strlen ("1234") +1); strcpy (ptr-> dn, "1234"); # 45 "macro_fun2.cpp" switch (ptr-> type) { case MonitorID : {CBMonitor * pthis = static_cast (ptr); if (pthis & & strcmp (pthis-> dn, "1234") == 0) Monitor = Query; else Monitor = 스냅 샷; 휴식; }; }

return 0; }

*** By the way I changed code to to avoid the massive printing of iostream by preprocessor - otherwise -E printing would have been huge. 

#define COMPARE(id, thismsg) case id##ID: { \ 
    CB##id * pthis = static_cast<CB##id *>(thismsg); \ 
    if(pthis && strcmp(pthis->dn, "1234") == 0) \ 
     id = Query; \ 
    else \ 
     id = Snapshot; \ 
    break; } \ 
+0

* 문제 * 무엇입니까? – Puppy

+2

우선 stringification 연산자는 단일 '#'이며이 코드의 아무 곳에 나 나타나지 않습니다. –

+0

먼저 무엇을 제안 하시겠습니까? 무슨 일이 일어나는지 보려면 출력을 전처리하는 것입니다. GCC를 사용하는 경우 -E 옵션을 사용하여 수행 할 수 있습니다. 왜 여전히 문제가 발생하면 사전 처리 된 출력을 여기에 붙여넣고 알려주십시오. http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/Preprocessor-Options.html#Preprocessor-Offions – Salgar

답변

0

id##ID은 정의되지 않은 MonitorID으로 확장됩니다. Monitorrqtypes의 정의로 MonitorID으로 바꾸거나이 값을 id으로 변경하십시오.

CB##id 또한 정의되지 않은 CBMonitor으로 확장됩니다. class CBMonitorDeviceRequestclass CBMonitor으로 바꾸거나이 값을 CB##id##DeviceRequest으로 변경하십시오.

그 외에도 명백한 문제는 볼 수 없습니다. 물론 메모리 누출과는 별도로 문자열에 std::string을 사용하지 않는 이유는 무엇입니까?

1

난 당신이 CB##id하지 CBid## 싶은 생각합니다.

+0

감사합니다. 나는 또한 방금 추가 한 오류가 있습니다. –