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

Undefined reference error in C++ code

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

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++ 的新手,我真的不知道出了什么问题。你有什么建议?

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

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

成员函数定义时需要加上class名称前缀。