在此范围内未声明“数据”

时间:2013-10-16 18:27:44

标签: c++ arrays constructor destructor goertzel-algorithm

我正在用C ++实现Goertzel算法,到目前为止我已经做到了:

#include <iostream>
#include <fstream>
#include <string>
#include <math.h>

const double pi = 3.14159;

class digitalFilter{
    private:
        int     k,i;
        float   temp;
        float   scalingFactor;
        float   floatnumSamples;
        float   omega,sine,cosine,coeff,q0,q1,q2,magnitude,real,imag;
        int const static limit = 205;
    public:
        digitalFilter();
        void readDataFromFile();
        float goertzel_mag(int numSamples,int TARGET_FREQUENCY,int SAMPLING_RATE, float* data);
};

digitalFilter::digitalFilter(){
    float* data = new float[limit];
}

void digitalFilter::readDataFromFile(){
    //get 205 values from txt file and store this in an array
    std::ifstream dataFile ("data_files/datad.txt");
    if (dataFile.is_open()){
        for (int i = 0; i < limit; i++){
            dataFile >> temp;
            data[i] = temp;
        }
        dataFile.close();
    }else{
        std::cout << "Unable to open file\n";
    }
}

float digitalFilter::goertzel_mag(int numSamples,int TARGET_FREQUENCY,int SAMPLING_RATE, float* data){
    scalingFactor = numSamples / 2.0;

    floatnumSamples = (float) numSamples;
    k = (int) (0.5 + ((floatnumSamples * TARGET_FREQUENCY) / SAMPLING_RATE));
    omega = (2.0 * pi * k) / floatnumSamples;
    sine = sin(omega);
    cosine = cos(omega);
    coeff = 2.0 * cosine;
    q0=0;
    q1=0;
    q2=0;

    for(int i = 0; i < numSamples; i++)
    {
        q0 = coeff * q1 - q2 + data[i];
        q2 = q1;
        q1 = q0;
    }

    // calculate the real and imaginary results
    // scaling appropriately
    real = (q1 - q2 * cosine) / scalingFactor;
    imag = (q2 * sine) / scalingFactor;

    magnitude = sqrtf(real*real + imag*imag);
    std::cout << "a" << TARGET_FREQUENCY << " = " << magnitude << std::endl;
    return magnitude;
}


int main()
{
    digitalFilter ObjOne;
    //compute amplitude magnitude of DFT
    ObjOne.readDataFromFile();
    ObjOne.goertzel_mag(205,697,8000,data);
    ObjOne.goertzel_mag(205,770,8000,data);
    ObjOne.goertzel_mag(205,852,8000,data);
    ObjOne.goertzel_mag(205,941,8000,data);
    ObjOne.goertzel_mag(205,1209,8000,data);
    ObjOne.goertzel_mag(205,1336,8000,data);
    ObjOne.goertzel_mag(205,1477,8000,data);
    ObjOne.goertzel_mag(205,1633,8000,data);

    return 0;
}

但我得到了这些愚蠢的错误代码:

goertzel_mag-v3.cpp: In member function 'void digitalFilter::readDataFromFile()':
goertzel_mag-v3.cpp:41:4: error: 'data' was not declared in this scope
goertzel_mag-v3.cpp: In function 'int main()':
goertzel_mag-v3.cpp:85:35: error: 'data' was not declared in this scope

我完全迷失了,为什么我的成员函数readDataFromFile()找不到我的数组data[]?似乎构造函数构建数组,然后在程序离开构造函数后销毁它?我怎样才能解决这些显然是愚蠢的错误?

2 个答案:

答案 0 :(得分:4)

digitalFilter::digitalFilter(){
    float* data = new float[limit];
}

退出构造函数后,data数组不再存在。使其成为类成员变量或保留它的全局变量。

答案 1 :(得分:0)

将float *数据初始化为成员函数将解决第一个问题。 因为它在构造函数中初始化,并且只能用于该块并超出范围

对于主要部分,一旦将其作为成员函数,就使用该对象访问它。 例如:obj.data