2012-10-09 23 views
0

我在C++關於引用的方法

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); 
}; 

我得到那個說不能聲明成員的錯誤函數靜態浮動Data :: computeData(std :: string,int,int,float,float)有靜態鏈接[-fpermissive]

then then .. at

Main.cpp的

我試圖使用功能

#include "File2.h" 

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

,它給我computeData沒有在這個範圍內聲明..

衷心感謝所有幫助!

回答

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 
2

這些是static兩種含義。在類定義中僅聲明一個靜態成員函數,並且意味着該函數在運行時不指定this指針(即它是一個正常的函數,恰好可以訪問類File2中的私有數據)。在其定義中聲明函數staticstatic的C語法,並且表示該函數在其當前文件外部不可見/可鏈接。在C++中,成員函數不能有靜態鏈接。不要把static放在靜態成員函數的定義中。