C ++代码中未定义的引用错误

时间:2015-03-21 19:47:15

标签: c++ compilation

您好我编译代码时遇到此错误:

main.cpp:(.text.startup+0xfc): undefined reference to `CMyMath::melFilterBank(std::vector<double, std::allocator<double> >, int, int, int)'
collect2: error: ld returned 1 exit status
make: *** [bin/main.elf] Error 1

我的.h文件:

#ifndef _MYMATH_H_
#define _MYMATH_H_
#define _USE_MATH_DEFINES
#include <vector>
#include <stdio.h>
#include <cmath>
#include <stdint.h>
#include <complex> 
class CMyMath
{
    public:
        CMyMath();  
        ~CMyMath();
        std::vector<double> melFilterBank(std::vector<double> signal, int frequency, int band_num, int coef_num);
};
#endif

我的.cpp文件:

#include "MyMath.h"    
CMyMath::CMyMath()
{
    printf("constructor called\n");
}   
CMyMath::~CMyMath()
{
    printf("destructor called\n");
}
std::vector<double> melFilterBank(std::vector<double> ourSignal, int frequency, int bandNum, int coefNum)
{
    std::vector<double> output; //ck in matlab code
    /*
    DO SOME STUFF
    */
    return output;
}

主:

#include <stdio.h>
#include <vector>
#include <cmath>
#include "MyMath.h"

int main()
{
    class CMyMath a;
    std::vector<double> mel {0.0000001,0.0000005,0.0000004,0.0000005};
    a.melFilterBank(mel,8000,6,5);
    return 0;
}

你认为哪里应该是一个错误?我是C ++的新手,我真的不知道出了什么问题。你有什么建议?

2 个答案:

答案 0 :(得分:1)

定义(在.cpp文件中)需要指定您定义成员函数,而不是单独的非成员函数:

std::vector<double> CMyMath::melFilterBank(std::vector<double> ourSignal, int frequency, int bandNum, int coefNum)
                    ^^^^^^^^^

答案 1 :(得分:1)

std::vector<double> CMyMath :: melFilterBank(std::vector<double> ourSignal, int frequency, int bandNum, int coefNum)

成员函数在定义时需要以类名为前缀。