2015-11-10 9 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) { 
    //... 
相关问题