Declaring a Constructor

Hello There!
I'm working on a histogram class, but something seems to be wrong with the constructor I'm using...

here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class histo
{
      private:
              int nBin_p;       
              float min_p, max_p; 
              int *BinContent_p;
              float step_p;
              float sum_p;      
      
      public:
            histo();       //default constructor  
            histo(int &nBin, float &min, float &max);      //constructor
            ~histo();              //destructor
            
            int fill(const float &val);
            void print()const;
            float getMean()const;
            
};


histo::histo(int &nBin, float &min, float &max)
{
            nBin_p=nBin;
            min_p=min;
            max_P=max;         
            step_p=(max_p-min_p)/nBin_p;
            sum_p=0.;
            BinContent_p=new int[nBin_p];
            for (int i=0; i<nBin_p; i++)
                BinContent_p[i]=i;  
                
}

histo::~histo()
{
               delete BinContent_p[nBin_p];
}


I get an error message at line 12 saying:
" expected `,' or `...' before ';' token "
" expected `)' before ';' token "
"expected unqualified-id before "const" "

is it just a semicolon mistake somewhere? or am i getting const correctness wrong??


This is unrelated to your problem, but you should not be passing by reference. The constructor should look like this:

histo(int nBin, float Min, float Max);

note I got rid of the &'s.

As for your error... are you #including <windows.h>? If yes, it #defines min and max, which means you can't use those as identifiers in your code. Note I changed the names above to 'Min' and 'Max' to avoid this conflict.
Thanks!!

why shouldn't I pass references though?
Ps...I only #included <iostream>
Generally one passes by reference in one of two cases: to save large amounts of space by avoiding the copying of a large variable (in which case a const is generally used for safety reasons), or if there's an intention of changing a variable being passed to the function. Neither appears to apply here, so there's no real need to pass by reference. :)

-Albatross
Last edited on
Thank you!
my compiler still refuses to look at it though...any idea as to why?
I tried changing min, max to Min, Max, but it still won't work..
Is something wrong with the declaration of the constructor?
Did you change them in both places? (the class and the definition)

What is the exact error you are getting?
Is something wrong with the declaration of the constructor?
no. I can compile it. But line 26 and 37 are wrong
ok!.. I thought 37 wasn't right...it should jut be:

delete BinContent_p;

right?

But why is line 26 wrong? in that case, are 24 and 25 wrong too?

the errors I get are:

" expected `,' or `...' before ';' token "
" expected `)' before ';' token "
"expected unqualified-id before "const" "

both referred to line 12

Could you paste the code as you have it now?

Oh, and if you use new[] you need to use delete[] to match, just a normal delete doesn't work:

delete [] BinContent_p;
Last edited on
Also, I'm writing all this in a header file, and i wrote this:

1
2
3
4
5
6
7
8
9
#include<iostream>
using namespace std;

#ifndef histo_h
#define histo_h

class histo
{....


Is Something wrong about this??I'm not very confident about using header files yet...
Ok...this is the code as I have i now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef histo_h
#define histo_h

#include<iostream>
using namespace std;



class histo
{
      private:
              int nBin_p;      
              float min_p, max_p; 
              int *BinContent_p;
              float step_p;
              float sum_p;   
      
      public:
            histo();                                                  //default constructor
            histo(int nBin, float Min, float Max);      //constructor
            ~histo();                                              //destructor
            
            //int fill(const float &val);
            void print()const;
            //float getMean()const;
            
};


histo::histo(int nBin, float Min, float Max)
{
            nBin_p=nBin;
            min_p=Min;
            max_P=Max;         
            step_p=(max_p-min_p)/nBin_p;
            sum_p=0.;
            BinContent_p=new int[nBin_p];
            for (int i=0; i<nBin_p; i++)
                BinContent_p[i]=i;  
                
}

histo::~histo()
{
               delete [] BinContent_p;
}
Oh...and it's the header file, saved as program-12.h :D
You seem to be missing an #endif in that file, but I'm guessing that's just a copy/paste error.

As for your code...I don't really see anything wrong.
yeah...you're right: the #endif was later on...

groaaaaan!!! it still won't compile :(

Thank you any way for your help ;o)
Crippy wrote:
ok!.. I thought 37 wasn't right...it should jut be:

delete BinContent_p;

right?
yes

Crippy wrote:
But why is line 26 wrong? in that case, are 24 and 25 wrong too?
line 26 is wrong because you wrote max_P which should be max_p // note: low case p . line 24/25 are ok.

for line 12. I don't have any explanation other than the compiler is wrong or macros. Try other names
what do you mean by macros?
I made two changes to the code posted at 9:42 (added #endif to the end and changed max_P to max_p on line 34) and it compiles without any problem into a library.
Last edited on
Topic archived. No new replies allowed.