简体   繁体   English

main.cpp中的错误:库中的函数“未在此范围内声明”,但实际上是

[英]Error in main.cpp: function in library “not declared in this scope”, but actually it is

I ask for an help since I've read tons of similar Q&A, but none of them actually did help me. 我要求帮助,因为我已经阅读了大量类似的问答,但他们都没有帮助我。

I wrote a library (libreria.h), with its "libreria.cc" and a main.cpp. 我写了一个库(libreria.h),它的“libreria.cc”和一个main.cpp。 Everything seems to be fine, but when I try to compile (I'm doing it on Ubuntu Terminal), it says: "integral_area was not declared in this scope". 一切似乎都很好,但是当我尝试编译时(我在Ubuntu终端上进行编译),它说:“在此范围内未声明integral_area”。

My code is supposed to calculate mean and variance of the area under a mathematical function (which actually is the value of the integral of that function). 我的代码应该计算数学函数下面积的均值和方差(实际上是该函数积分的值)。

To be clear, if I put the single functions above the main, the code works. 要清楚,如果我把单个函数放在main之上,代码就可以了。 A problem is when I try to put them in an external library. 问题是当我尝试将它们放在外部库中时。

Here's the code (I changed the var names from Italian to English, if there's some typing error don't mind them, please, because the original code doesn't have them. I checked almost a billion times. The problem is somewhere else): 这是代码(我将var名称从意大利语更改为英语,如果有一些输入错误,请不要介意它们,因为原始代码没有它们。我检查了近十亿次。问题出在其他地方) :

libreria.h libreria.h

#ifndef libreria_h
#define libreria_h

class libreria
{
public:
    ~libreria();

    double random ();
    double randomrange (double xmin, double xmax);

    double matfunc(double x);

    double integral_area(double xmin, double xmax,double ymin, double ymax);


private:

    double x;
    double xmin;
    double xmax;
    double ymin;
    double ymax;
    int N;
};
#endif

libreria.cc libreria.cc

#include "libreria.h"
#include <iostream>
#include <cstdlib>
using namespace std;


double libreria::random (){

    return (rand() / (1.*RAND_MAX));
}

double libreria::randomrange (double xmin, double xmax){

    return (xmin + (xmax-xmin) * rand() / (1.*RAND_MAX));
}

double libreria::matfunc(double x){

    return (exp(-1*x*x*4)); 
}

double libreria::integral_area(double xmin,double xmax,double ymin,double ymax){
    double x = 0.;
    double y = 0.;
    do { 
        x= randomrange(xmin,xmax);
        y= randomrange(ymin,ymax);  }while(y > matfunc(x)); 
    return(x);
}

libreria::~libreria(){ //do nothing
}

main.cpp main.cpp中

#include <cstdlib>
#include <iostream>
#include <ctime>
#include <iomanip>
#include <math.h>
#include "libreria.h"
#include "libreria.cc"
using namespace std;

int main () {

    srand(time(NULL));

    double ax,bx,ay,by; // [ax,by] [ay,by]
    double N,p;
    double mean=0,meanq=0,variance=0;

    cin >> N;

    cin >> ax >> bx;

    cin >> ay >> by;

    for(int i=1; i<=N; i++){

        p = integral_area(ax,bx,ay,by);
        cout << "Val " << i << ": " << setprecision(2) << p << endl;
        mean+=p;   
        meanq+=(p*p); 

        p=0;
    }


    mean=mean/N;
    cout << "Mean is: " << setprecision(2) << mean << endl;

    meanq=meanq/N;
    variance=(meanq- (mean*mean));
    cout << "Variance is: " << setprecision(2) << variance << endl;
}

libreria is a class, and you have not declared an instance of it. libreria是一个类,你还没有声明它的实例。 You need to do something like: 你需要做一些事情:

libreria l; // at the top

// rest of code.....
l.integral_area(ax,bx,ay,by);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM