簡體   English   中英

main.cpp中的錯誤:庫中的函數“未在此范圍內聲明”,但實際上是

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

我要求幫助,因為我已經閱讀了大量類似的問答,但他們都沒有幫助我。

我寫了一個庫(libreria.h),它的“libreria.cc”和一個main.cpp。 一切似乎都很好,但是當我嘗試編譯時(我在Ubuntu終端上進行編譯),它說:“在此范圍內未聲明integral_area”。

我的代碼應該計算數學函數下面積的均值和方差(實際上是該函數積分的值)。

要清楚,如果我把單個函數放在main之上,代碼就可以了。 問題是當我嘗試將它們放在外部庫中時。

這是代碼(我將var名稱從意大利語更改為英語,如果有一些輸入錯誤,請不要介意它們,因為原始代碼沒有它們。我檢查了近十億次。問題出在其他地方) :

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

#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中

#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是一個類,你還沒有聲明它的實例。 你需要做一些事情:

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