1

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;
}
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Kiki
  • 13
  • 3
  • wait there is a class, nothing static, where is the _instance_ of `libraria` that you would call ::integral_area from? – Cee McSharpface Feb 08 '17 at 10:17
  • the method `integral_area` is a member of a class, and you're not referencing the class in the code - e.g. `libreria l; p = l.integral_area(ax,bx,ay,by)`. – Anya Shenanigans Feb 08 '17 at 10:17
  • 1
    In your main.cpp, you included `"libreria.cc"`. It's very unusual to include C++ source files. Error or typo? – Scheff's Cat Feb 08 '17 at 10:17
  • Welcome to Stack Overflow! Please [edit] your code to reduce it to a [mcve] of your problem. Your current code includes much that is peripheral to your problem - a minimal sample normally looks similar to a good unit test: only performing one task, with input values specified for reproducibility. – Toby Speight Feb 08 '17 at 10:32
  • Welcome to Stack Overflow! To help people answer your question, you'll need to be more specific about the error. Please [edit] your post to incorporate the exact errors you get from your [mcve] (preferably using copy+paste to avoid transcription errors). – Toby Speight Feb 08 '17 at 10:32
  • Ok Toby! Thank you all for the replies and the tips! – Kiki Feb 08 '17 at 15:01

1 Answers1

0

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);
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
  • Omg. Thanks A LOT! In my defense, I can say that my teacher didn't specify this passage. I know this is awkward, but I checked my slides, it's no written. Thanks again – Kiki Feb 08 '17 at 10:37