2016-07-08 4 views
-1

の定義は、私は3つのファイルを持っています。 gp_frame.hにクラスを宣言し、メンバ関数をgp_frame.cppに定義し、クラスをmain.cppにしたいと考えています。大まか独立インラインクラスのメンバ関数の宣言と異なるファイル(ヘッダおよびソース)

、三つのファイル:次に

/*main.cpp*/ 
#include "gp_frame.h" 

void plotpicture(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int e){ 
anita.wait_enter("Some string\n"); 
} 

int main(){ 
gp_frame anita(true); 
plotpicture(1,2,3,4); 
return 0; 
} 

/*gp_frame.h*/ 

class gp_frame{ 
public: void wait_enter(std::string uzi); 
gp_frame(); 
gp_frame(bool isPersist); 
}; 

/*gp_frame.cpp*/ 
#include "gp_frame.h" 

void gp_frame::wait_enter(std::string uzi){ 
/*Some of code*/ 
} 
gp_frame::gp_frame(){ 
/*Some of code*/ 
} 
gp_frame::gp_frame(bool isPersist){ 
/*Some of code*/ 
} 

、私はファイルをコンパイルし、リンクしたい:

g++ -c main.cpp -w -Wall 
g++ -c gp_frame.cpp -w -Wall 
g++ gp_frame.o main.o -o Myprogram 

そしてすべてが OKです。

/*in gp_frame.h*/ 
public: inline void wait_enter(std::string uzi); 
/*in gp_frame.cpp*/ 
inline void gp_frame::wait_enter(std::string uzi){ 
/*Some of code*/ 
} 

コンパイラが同様に動作しますが、リンカは私にエラーがスローされます:

main.o: In function `plotpicture(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)': 
main.cpp:(.text+0x2c6b): undefined reference to `gp_frame::wait_enter(std::string)' 
collect2: error: ld returned 1 exit status 

はあなたが私どのように説明できますが、私が宣言したい場合は、/のようなinlineとして機能wait_enterを定義します問題を解決するか、何が間違っていますか?


悲しいことに、extern inlinestatic inlineどちらも私の問題を解決します。

+0

ヘッダーにインライン関数を定義する必要があります。実装でのみ定義されているインライン関数を持つことは意味がありません。コンパイラはどのように定義してインライン展開することができますか? –

+0

インライン関数を呼び出すときに[未定義参照の重複があります](http://stackoverflow.com/questions/19068705/undefined-reference-when-calling-inline-function) –

答えて

3

what do I wrong?

あなたは、インライン関数gp_frame::wait_enter(std::string)を宣言しますが、規格で要求される機能を使用してコンパイル単位(ソースファイル)のすべての関数を定義していませんでした。

特に、main.cppで関数を使用していても、では関数を定義したにもかかわらず、main.cppではなく関数を定義しました。

how to solve the problem

インライン関数を使用するすべてのコンパイル単位でインライン関数を定義します。それを行う慣用的な方法は、それらを宣言する同じヘッダに定義することです(この場合はgp_frame.h)。

関連する問題