c++ - 快速傅立叶变换 : using templated derived classes 'undefined reference' error even though header and . cpp 文件匹配

标签 c++ vector fft allocator complextype

我正在编写一个快速傅里叶变换算法,该算法使用复 vector 递归地执行 Cooley Tukey 方法。头文件和 .cpp 文件在参数名称和语法方面匹配,但我仍然收到“ undefined reference ”错误。由于错误中提到了“额外的”分配器参数。我认为这可能与为我们的抽象基类和派生的 Cooley-tukey 类使用模板有关。所有的问题都来自 FFF_REC 函数,该函数递归地划分输入。

Github:https://github.com/ProgrammerB/Fourier-Transform-Terminal-/blob/master/classes/cooley-tukey.h

我已经尝试将我的参数更改为引用并将私有(private)成员添加到 cooley-tukey 类中,但我遇到了同样的错误。

Cooley-Tukey 类:

template<typename T>
class Cooley_tukey: protected Fourier<T>{
public:
    Cooley_tukey();
    Cooley_tukey(std::string file_name, double frequency, double, 
      frequency_step, std::string output_name);
    //~Cooley_tukey();

    void FFT(const std::vector<T> &index, const std::vector<T> &value);

    std::vector<complex<T>> FFT_REC(std::vector<complex<T>> &temp, int 
      total_time); //recursion function for FFT

private:
    int total_time;

};

部分错误:

classes\cooley-tukey.cpp:91:10: error: no matching function for call to 
'Cooley_tukey<double>::FFT_REC(std::vector<std::complex<double>, 
std::allocator<std::complex<double> > > [(total_time / 2)], int, 
std::vector<std::complex<double>, std::allocator<std::complex<double> > 
>&)'FFT_REC(odd, total_time/2, result);

FFT-递归函数(错误来源):

    template<typename T>
    std::vector<complex<T>> Cooley_tukey<T>::FFT_REC(std::vector<complex<T>>& temp, int total_time)
    {
        // Check if it is split up enough
        if (total_time >= 2)
        {

            // Split even and odds up
            std::vector<complex<T>> odd[total_time/2];
            std::vector<complex<T>> even[total_time/2];
            for (int i = 0; i < total_time / 2; i++)
            {
                even->at(i) = temp.at(i*2);
                odd->at(i)  = temp.at(i*2+1);
            }

            // Split up tasks through FFT recursion method
            FFT_REC(even, total_time/2);
            FFT_REC(odd, total_time/2);


            // DFT portion of FFT - calculates after everything has been split up through FFT_REC
            for (int frequency = 0; frequency < total_time / 2; frequency += this->frequency_step)
            {
                std::complex<T> t = exp(std::complex<T>(0, -2 * M_PI * frequency / total_time)) * odd->at(frequency);

                //Result of Cooley-Tukey algorithm:
                    //*This gives us the frequency values at certain times
                temp.at(frequency) = even->at(frequency) + t;
                temp.at(total_time / 2 + frequency) = even->at(frequency) - t;

            }
        }
        return temp;
    }

    template class Cooley_tukey<double>;

最佳答案

您正在创建一个 vector 数组作为 even/odd 而您应该只有一个 vector 。该错误表明没有采用 vector 数组的方法。

我假设你的意思是:

// Split even and odds up
std::vector<complex<T>> odd;
std::vector<complex<T>> even;
odd.reserve(total_time/2);
even.reserve(total_time/2);
for (int i = 0; i < total_time / 2; i++)
{
    even.push_back(temp.at(i*2));
    odd.push_back(temp.at(i*2+1));
}

您还尝试使用 at() 将值设置为空 vector ,这会导致错误。 vector 可能没有分配任何存储空间,如果您尝试使用 at() 对它们进行索引,当它超出范围时您会得到一个异常。

关于c++ - 快速傅立叶变换 : using templated derived classes 'undefined reference' error even though header and . cpp 文件匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55886911/

相关文章:

c++ - 改进音乐频率分析仪的参数

c++ - OpenCV - OpenCV Mat 等效于 boost 矩阵 array_type

c++ - 发送或接收结构时的字节对齐

c++ - 使用CMake制作后如何将目录内容复制到构建目录中?

c++ - C++ 的自定义迭代器函数

matlab - FFT 和改变频率以及矢量化 FOR 循环

c++ - 第一个参数没有从 'A' 到 'A &&' 的已知转换

c++ - 如何将 STL vector 中的一系列元素设置为特定值?

c++ - 使用索引删除 std::vector 中的元素

iphone - 如何在iphone中查看FFT结果?