2013-03-20 43 views
0

全部:不知道如何使用彈出庫

感謝您的幫助。

我是新來的C選項解析,現在,我想要的是使用popt庫來解析每個參數,並prnit出來。

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

    using namespace std; 

    int main(int argc, const char* argv[]){ 

     char* dt1; 
     char* dt2; 

     struct poptOption { 
       const char * longName; /* may be NULL */ 
       char shortName;  /* may be ’\0’ */ 
       int argInfo; 
       void * arg;   /* depends on argInfo */ 
       int val;    /* 0 means don’t return, just update flag */ 
       char * descrip;  /* description for autohelp -- may be NULL */ 
       char * argDescrip;  /* argument description for autohelp */ 
      }; 
     struct poptOption optionsTable[]={ 
      {"start",'s',POPT_ARG_STRING,dt1,'s',"The date format should like YYYY-MM-DD.",0}, 
      {"end",'e',POPT_ARG_STRING,dt2,'e',"The date format should like YYYY-MM-DD.",0}, 
      //~ POPT_AUTOHELP 
      //~ {NULL,0,0,NULL,0} 
      }; 

     poptContext optCon; 
     optCon = poptGetContext (0, argc, argv, optionsTable, 0); 

      const char* portname = poptGetArg(optCon); 
     cout<<portname<<endl; 
     return 0; 
} 

當我編譯它,我得到了錯誤llike:

test.cpp: In function ‘int main(int, const char**)’
test.cpp:27: warning: deprecated conversion from string constant to ‘char*’
test.cpp:27: warning: deprecated conversion from string constant to ‘char*’
test.cpp:30: error: cannot convert ‘main(int, const char**)::poptOption*’ to ‘const poptOption*’ for argument ‘4’ to ‘poptContext_s* poptGetContext(const char*, int, const char**, const poptOption*, unsigned int)’

回答

0

我不認爲你應該在你的程序中定義的結構poptOption。該結構應該在popt包含文件中爲你定義。嘗試刪除該結構定義。

注意,我覺得你還需要去掉這一行:

//~ {NULL,0,0,NULL,0} 
+0

謝謝!克雷格,我明白了。 – Kuan 2013-03-27 16:12:01

0

正在報告此警告的原因是C語言的一個特徵,但在代碼中的錯誤是由於你如何正試圖使用​​彈出。

C中的類型(char *)和(const char *)是不同的類型。其中一個並不是真正的另一個,雖然C允許從一種類型到另一種類型但不會被炸掉,但任何體面的C編譯器都會給你一個警告。您可以用類型轉換來隱藏警告,但這通常是一個糟糕的主意。

C風格的字符串類型爲(const char *),並且您將其指定給poptOption中的字段descrip,該字段定義爲(char *)。這會引發編譯器警告,因爲現在,通過跟隨optionsTable數組的引用進入該內存的人可能會嘗試更改該字符串的內容。考慮到字符串被定義爲常量,這是一件很奇怪的事情。這就是爲什麼你會收到警告。

您的代碼中的錯誤是您正在使用彈出不正確,您自己的定義爲poptOption struct。如果你在第55行的the file that you include (popt.h)之內,你會看到poptOption struct,因爲它是由popt作者定義的。它是:

struct poptOption { 
    /*@[email protected]*/ /*@[email protected]*/ const char * longName; /* may be NULL */ 
    char shortName;   /* may be '\0' */ 
    int argInfo; 
    /*@[email protected]*/ /*@[email protected]*/ void * arg;   /* depends on argInfo */ 
    int val;    /* 0 means don't return, just update flag */ 
    /*@[email protected]*/ /*@[email protected]*/ const char * descrip;  /* description for autohelp -- may be NULL */ 
    /*@[email protected]*/ /*@[email protected]*/ const char * argDescrip; /* argument description for autohelp */ 
}; 

或刪除評論

struct poptOption { 
    const char * longName; 
    char shortName; 
    int argInfo; 
    void * arg;   
    int val;    
    const char * descrip; 
    const char * argDescrip; 
}; 

,你清楚地看到,即使是作者預期(爲const char *),而不是(的char *)你定義。

+0

謝謝,Edwin,我終於發現問題是我重新定義了struct potOption。 – Kuan 2013-03-27 16:12:36