2015-11-10 12 views
2

我有一個問題,我不能達到我的頭函數,我想調用它在我的主要功能,但它說。 錯誤2錯誤C2039:'測試':不是'std :: basic_string < _Elem,_Traits,_Alloc>' 和未定義的類的成員爲什麼會發生這種情況? 注意:我刪除了代碼中不重要的部分。未定義的類,無法從主要到我的標題

#include "CompressHeader.h" 
    int main() 
    {  input.get(ch); 
     string a=ch; 
      if(Test(a))//here is undefined one. 
      { 
      } 

我的頭

class Compress 
{ 
public: 
    Compress(); 
    Compress(string hashData[],const int size); //constructor 
    void makeEmpty(); 
    bool Test(string data);//To test if data in the dictionary or not. 
+0

'temp2'從哪裏來? – Downvoter

+0

請**用[mcve]或[SSCCE(Short,Self Contained,Correct Example)](http://sscce.org)**您的問題 – NathanOliver

+0

它來自ifstream input.get(ch ); –

回答

1

因爲測試是壓縮的成員函數,所以你需要通過壓縮的一個實例調用,如:

string a=temp2; 
Compress c; 
if (c.Test(a)) {...} 

或把這段代碼裏面Compress的成員函數

+0

當然!謝謝! –

1

在以下代碼中:

if(Test(a))//here is undefined one. 

您稱爲全局函數Test - 實際上它是Compress類的成員。因此,要解決你的代碼,你應該調用測試壓縮對象:

Compress c; 
if (c.Test(a)){} 
+0

它非常感謝! –

2

因爲你要調用的方法不是static方法,你需要創建一個類Compress的對象(實例)來調用該方法,如:

#include "CompressHeader.h" 
int main() 
{ 
    // temp2 is not defined in your example, i made it a string 
    string a = "temp2"; 

    //Create the object 
    Compress compressObject; 
    //Call the method using the object 
    if(compressObject.Test(a) { 
    //... 
相關問題