2016-11-04 219 views
0

如果我宣佈一個函數在一個名爲ppmformat.h錯誤 '的extern' 後來 '靜態'

//file ppmformat.h 
namespace imaging 
{ 
Image * ReadPPM(const char * filename); 
} //namespace imaging 

文件...在ppmformat.cpp定義它

static imaging::Image * imaging::ReadPPM(const char *filename) 
{ 
.... 
} 

我得到以下錯誤:

'imaging::Image* imaging::ReadPPM(const char*)' was declared 'extern' and later 'static' [-fpermissive]

//ppmformat.h 
    #ifndef _PPM 
    #define _PPM 

    #include "Image.h" 
    namespace imaging 
    { 
    /*! Reads a PPM image file and returns a pointer to a newly allocated     Image object containing the image. 
    * 
    * \param filename is the null-terminated string of the name of the file to open. 
    * 
    * \return a pointer to a new Image object, if the read operation was successful, nullptr otherwise. 
    */ 
    Image * ReadPPM(const char * filename); 
    } //namespace imaging 

    #endif 

    //ppmformat.cpp 
    #include <iostream> 
    #include<string.h> 
    #include<stdio.h> 
    #include<stdlib.h> 
    #include <string> 
    #include <fstream> 
    #include "ppmformat.h" 

    using namespace std; 

    imaging::Image * imaging::ReadPPM(const char *filename) 
    { 
    ...... 
    } 

    //Image.h 

    #ifndef _IMAGE 
    #define _IMAGE 

    #include "Color.h" 
    #include <iostream> 

    namespace imaging 
    { 
    class Image 
    { 
    .... 
    } 
    } 

    //Image.cpp 
    #include <iostream> 
    #include "Color.h" 
    #include "Image.h" 
    .... 
    //end of Image.cpp 

    //Color.h 
    #ifndef _COLOR 
    #define _COLOR 
    namespace imaging 
    { 
    Class Color 
    { 
    .... 
    } 
    } 
+1

爲什麼要在源文件中定義函數'static'? –

+0

好問題,upvoted。 –

+0

如果我刪除靜態我採取「multiple :: definition :: ReadPPM(char const *)」的定義!你認爲包含文件有問題嗎? – madrugadas25845

回答

1

代碼與差異聲明相同的實體以前的聯繫是無效的。

C++ 11§7.11/ 8(dcl.std/8):

The linkages implied by successive declarations for a given entity shall agree. That is, within a given scope, each declaration declaring the same variable name or the same overloading of a function name shall imply the same linkage. Each function in a given set of overloaded functions can have a different linkage, however.

對於在實踐中,G ++ 5.1.4拒絕編譯,而不幸的是視覺C++ 2015接受它作爲語言擴展,一個警告:

 
[C:\my\forums\so\257] 
> g++ foo.cpp 
foo.cpp: In function 'imaging::Image* imaging::ReadPPM(const char*)': 
foo.cpp:9:62: error: 'imaging::Image* imaging::ReadPPM(const char*)' was declared 'extern' and later 'static' [-fpermissive] 
static imaging::Image * imaging::ReadPPM(const char *filename) 
                  ^
foo.cpp:5:13: note: previous declaration of 'imaging::Image* imaging::ReadPPM(const char*)' 
    Image * ReadPPM(const char * filename); 
      ^
foo.cpp: At global scope: 
foo.cpp:9:54: warning: unused parameter 'filename' [-Wunused-parameter] 
static imaging::Image * imaging::ReadPPM(const char *filename) 
                ^

[C:\my\forums\so\257] 
> cl foo.cpp 
foo.cpp 
foo.cpp(10): warning C4211: nonstandard extension used: redefined extern to static 
foo.cpp(9): warning C4100: 'filename': unreferenced formal parameter 
foo.cpp(9): warning C4505: 'imaging::ReadPPM': unreferenced local function has been removed 

[C:\my\forums\so\257] 
> _ 

與同警告接受這個編譯器,它可以把這一警告變成硬錯誤是一個好主意。使用Visual C++的選項/we4211

+0

當我在visual studio 2015編譯它是好的!用cmcc編譯的 ,我用這個錯誤! – madrugadas25845

+0

@ madrugadas25845:您可能需要在Visual Studio項目屬性中設置警告級別。我用下面的'CL'(環境變量)值進行編譯:'CL =/nologo/EHsc/GR/W4/FI「iso646.h」'。 –

+0

/W4通常可能有問題。當然,在過去,許多MS頭文件在/ W4處發出了警告 - 實際上很多警告都是隱藏在自己的代碼中的。在過去我傾向於/ W3,然後我可以打開/ Werror。 –