0

で関数を取得しました

File2.cpp which contains the following code below

#include "File2.h"
//some codes in between

static float File2::computeData(string s,int a,int b,float c,float d)
{
float result;
//the compute code
return result;
}

File2.h で、クラスで宣言しようとしました

Class File2
{
private:
//some variables
public:
static float computeData(string,int,int,float,float);
};

メンバ関数 static float Data::computeData(std::string,int ,int , float , float) を静的リンケージ [-fpermissive] に宣言できないというエラーが表示されます。

それからまた..私のところで

メイン.cpp

関数を使用しようとしていました

#include "File2.h"

float result;
result = computeData(string s,int a,int b,float c,float d);

そして、computeDataはこのスコープで宣言されていませんでした..

すべての助けに心から感謝します!

4

2 に答える 2

2

これらはの2つの意味ですstatic。静的メンバー関数の宣言はクラス定義でのみ行われ、関数が実行時にポインターを受け取らないことを意味thisします(つまり、クラス内のプライベートデータにアクセスできる通常の関数ですFile2)。定義で関数を宣言することstaticは、のC構文でstaticあり、関数が現在のファイルの外部で表示/リンクできないことを意味します。C ++では、メンバー関数は静的リンケージを持つことができません。static静的メンバー関数の定義を入れないでください。

于 2012-10-09T15:53:58.067 に答える
2

staticメンバー メソッドはstatic、クラスの外側ではなく、クラスの内側でのみ宣言します。定義は次のとおりです。

float File2::computeData(string s,int a,int b,float c,float d)
{
   float result;
   //the compute code
   return result;
}

staticクラス外のキーワードはありません。

クラス定義の外では、static静的メンバー関数では許可されていない内部 (または静的) リンケージを提供します。

class X
{
    static void foo(); //static class member
};

static void foo();     //static free function w/ internal linkage
于 2012-10-09T15:51:15.953 に答える