2016-11-04 2 views
0

私はエラー 'のextern' と、後に '静的'

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

ppmformat.hという名前のファイルに関数を宣言...と私が得る

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

ppmformat.cppでそれを定義した場合次のエラー:

'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

静的を削除する場合、 "imaging :: ReadPPM(char const *)"の多重定義を取る!インクルードファイルに問題があると思いますか? – madrugadas25845

答えて

1

コードの差分と同じエンティティを宣言他のリンケージは無効です。

C++ 11§7.11/ 8(/ 8 dcl.std):残念ながらビジュアルC++ 2015はとしてそれを受け入れながらにおけるプラクティス、G ++ 5.1.4について

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.

は、それをコンパイルすることを拒否する言語拡張は、警告:警告でこれを受け入れコンパイラで

 
[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

私はビジュアルスタジオ2015でコンパイルしたときは大丈夫でした! cmdで をgccでコンパイルしましたが、このエラーが発生します。 – madrugadas25845

+0

@ madrugadas25845:おそらく、Visual Studioのプロジェクトプロパティで警告レベルを上げる必要があります。 'CL =/nologo/EHsc/GR/W4/FI" iso646.h "'という次の 'CL'(環境変数)値でコンパイルしました。 –

+0

/W4は問題になることがよくあります。過去には確かに、多くのMSヘッダーが/ W4で警告を出しました。実際には、多くの場合、自分のコードの警告が見えなくなっていました。過去に私は/ W3に傾いていたので、私は/ Werrorをオンにすることができました。 –

関連する問題