简体   繁体   中英

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. 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".

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. 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):

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 is a class, and you have not declared an instance of it. You need to do something like:

libreria l; // at the top

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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