【问题标题】:'data' was not declared in this scope [closed]未在此范围内声明“数据”[关闭]
【发布时间】:2013-10-25 00:20:31
【问题描述】:

我正在用 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[]??似乎构造函数构建了数组,然后在程序离开构造函数后将其销毁?我该如何解决这些看似愚蠢的错误?

【问题讨论】:

  • 声明float* data和其他成员变量,不在构造函数中。
  • “我的数组”是什么意思?您在其他函数中声明的局部变量?
  • 哦,听起来你需要为初学者学习C++,然后再试一次:)
  • Kunal:我刚刚尝试过,它适用于我所有的成员函数,但不适用于我的主函数。我宁愿不全局声明,怎么办?
  • @NicolaiAntonLynnerup 您尝试在main() 中访问data 吗?

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


【解决方案1】:
digitalFilter::digitalFilter(){
    float* data = new float[limit];
}

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

【讨论】:

  • 是的,它可以作为全局变量,但不能作为类成员变量,因为我在 main 中使用它。
  • 好吧,只要满足您的需求 - 如果它是访问器在主函数中的成员变量,您就可以使用它。尽管您实际上不需要将其传递给成员函数,因为它们可以直接访问它。
【解决方案2】:

将 float *data 初始化为成员函数将解决第一个问题。 因为它是在构造函数中初始化的,并且仅对该块可用并且超出范围

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

【讨论】:

  • float *data 不是成员函数。进一步,是否可以在main()中访问取决于是public还是private
  • @Kunal 正是因此我要求他将其设为数据成员,并公开因为他在 main 中再次调用它
猜你喜欢
  • 2013-02-17
  • 1970-01-01
  • 2021-12-16
  • 1970-01-01
  • 2014-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多