未定义的课程,无法从主要地址到我的标题

时间:2015-11-10 21:06:50

标签: c++ class undefined member

我有一个问题,我无法在标题中找到我的功能,我想在我的主要功能中调用它,但它说明了。 错误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.

3 个答案:

答案 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) {
    //...

答案 1 :(得分:1)

因为Test是Compress的成员函数,所以你需要通过Compress实例调用,如:

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

或将此代码放在Compress

的成员函数中

答案 2 :(得分:1)

在下面的代码中:

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

你调用一个全局函数Test - 它实际上是Compress类的成员。因此,要修复代码,您应该调用Test on Compress对象:

Compress c;
if (c.Test(a)){}
相关问题