の定義は、私は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 inline
もstatic inline
どちらも私の問題を解決します。
ヘッダーにインライン関数を定義する必要があります。実装でのみ定義されているインライン関数を持つことは意味がありません。コンパイラはどのように定義してインライン展開することができますか? –
インライン関数を呼び出すときに[未定義参照の重複があります](http://stackoverflow.com/questions/19068705/undefined-reference-when-calling-inline-function) –