problems with header file (code to find extrema (turning points))

so.. my code is analysing some data and extracting the extrema.. (supposedly)


the structure of my code is as follows

MAIN
2dev.cpp
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

#include "bnewhead.h"

int main ()
{
    int y,i=1,n= 400 ;
    double in_data_sd, in_data_sd_toll, in_data_size, in_data_abs_toll;
    double out_flag_const_data;

    vector<double> in_data; //indata    

    ofstream out1("sinfn.dat");
    out_flag_const_data = 0;
    in_data_sd = 0;
    in_data_size = 0;

    in_data_abs_toll = 0.1;
    in_data_sd_toll = 0.001;  //analyse data initially

    for(y = 0; y < n; y++ )//generating the data to be analysed

    {   
        in_data.push_back(sin(y*.1 ) + 1);
    }

    in_data[30] = 9;

    in_data[40] = -2;   

    for(y = 0; y < n; y++ ) //sending to data to a file (so i can plot it and look at it) 

    {   
        out1 << y << "    " << in_data[y]<< "\n";
     }   


    in_data_sd  = stddev(in_data);


    extreme(in_data, in_data_sd, i,i);   //calling the function here

    return 0;

}


HEADER
bnewhead.h
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
#ifndef Header
#define Header

#include <iostream>
#include <math.h>
#include <fstream>    
#include <vector>
#include <complex>
using namespace std;


double square(double x ){return x*x; }


double stddev(vector<double> x )
{
    int i;
    double avg,size, deviation, sum, var;
    size = x.size();
    sum = 0;

    for(i = 0; i < size; i ++)
    {
        sum+= x[i];
    }

    avg = sum/x.size();

    for(i = 0; i< size; i++)
    {
        var += square(avg - x[i]);
    }

    deviation = sqrt(var/x.size());

    return deviation;
}


double extreme( vector<double> in_data, double std_dev, double omega1, double field)
    
#endif 



FUNCTION
extr.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100


double extreme( vector<double> in_data, double std_dev, double omega1, double field)
{
    

    int i;
    int flag_old_extrema;
    double in_data_sd, in_data_sd_toll, in_data_size, in_data_abs_toll;
    double out_flag_const_data = 0;
    vector<double> out_extrema;

    in_data_abs_toll = 0.1; //tollerance of the difference to specify a new extrema 


    in_data_sd_toll = 0.001;  //analyse data initially by getting the standard deviation 
                                              //(to see if itsmore or less constant

    in_data_sd  = std_dev;
    in_data_size = in_data.size();

    //if its constant data then we can just send out one number.. bish bash bosh
    if (in_data_sd < in_data_sd_toll)
    {
        out_flag_const_data = 1;
        out_extrema.push_back(in_data[in_data_size]);
    }

    //if it isn't constant then we have loop over all the data
    else
    { 
        for(i= 0; i < in_data_size - 1; i++)
        {
            if ((in_data[i] > in_data[i-1]) && (in_data[i]>in_data[i+1])) 
            {
                if (out_extrema.size() == 0)
                {
                    //First extrema
                    out_extrema.push_back(in_data[i]);
                }

                // possible new extremum
                else
                {
                    flag_old_extrema = 0;

                    if ( abs(in_data[i] - out_extrema[i-1]) <= in_data_abs_toll ) 
                    { 
                        flag_old_extrema  = 1;
                    } 

                    if (flag_old_extrema  == 0)
                    {
                        out_extrema.push_back(in_data[i]);
                    }
                }
            } 

            if ((in_data[i] < in_data[i-1]) && (in_data[i] < in_data[i+1])) 
            {
                if (out_extrema.size() == 0)
                {
                    //First extrema
                    out_extrema.push_back(in_data[i]);
                }

                // possible new extremum
                else
                { flag_old_extrema = 0; 
                    if ( abs(in_data[i] - out_extrema[i-1]) <= in_data_abs_toll ) 
                    { 
                        flag_old_extrema  = 1;
                    } 

                    if (flag_old_extrema  == 0)
                    {
                        out_extrema.push_back(in_data[i]);
                    }
                }
            }
        }
    }

    if(field == 1) {
        for(i = 0; i < out_extrema.size(); i++)
        {
            cout<< i<< "         " <<  out_extrema[i] << "\n";
        }
    }

    if(field == 2) {
        for(i = 0; i < out_extrema.size(); i++)
        {
            //       out2<< omega1<< "         " <<  out_extrema[i] << "\n";
        }
    }

    return 0;

}



lnx0023ae8648e1 (87) g++ extr.cpp 2dev.cpp -Wall
extr.cpp:4: error: 'vector' was not declared in this scope
extr.cpp:4: error: expected primary-expression before 'double'
extr.cpp:4: error: expected primary-expression before 'double'
extr.cpp:4: error: expected primary-expression before 'double'
extr.cpp:4: error: expected primary-expression before 'double'
extr.cpp:4: error: initializer expression list treated as compound expression
extr.cpp:5: error: expected ',' or ';' before '{' token
2dev.cpp:7: error: expected initializer before 'int'




from the error statements it appears that there is something wrong with the header file, but i can't understand what the problem is...

when i put the function which is causing trouble in the main code it works fine... without any problems...

anyone any ideas>!?
Last edited on
You are not #including anything in extr.cpp so it doesn't know about std::vector.
In the header you have the function bodies of square and stddev but you should move them to a cpp file.
And you shouldn't be using namespace std; in a header
thanks for the reply bazzy!



Should I #include the same header file in my extr.cpp file?

I need to include the header file in all the functions I use in my main code?

Should I just move the 'using namespace std' into each individual .cpp file?

sorry for all the questions.. the whole area of namespaces has confused me a huge amount!!

thanks again
Should I #include the same header file in my extr.cpp file?
Yes
I need to include the header file in all the functions I use in my main code?
If you mean "do I have to #include the header in all the files which implement functions declared in it?" then yes
Should I just move the 'using namespace std' into each individual .cpp file?
Yes, not doing it won't make your program fail to compile but it's good practice
Yes, not doing it won't make your program fail to compile but it's good practice





cool, in what type of case would it cause trouble?
When you are using different libraries

If you use the namespace in the cpp files, it's quite clear that eg vector is std::vector but think if you have this situation:
1
2
3
4
5
6
//MyHeader.h
#include <OtherLibrary/vector.hpp>

using namespace OtherLibrary;

//... 

1
2
3
4
5
6
//MySource.cpp
#include <vector>
#include "MyHeader.h"
using namespace std;

vector<int> V; // ambiguity: std::vector or OtherLibrary::vector? 
Thanks again bazzy.. I got it working eventually!


What I ended up doin was putting all of my functions in a single .cpp file... but I still ended up putting using namespace std; in the header file (it wouldn't work otherwise)


my final (more or less working) code:


MAIN

3dev.cpp
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

#include "3newhead.h"
using namespace std; 

int main ()
{
    int y,i=1,n= 400 ;
    double in_data_sd, in_sd_toll,in_abs_toll,average;
    vector<double> in_data; //indata    
    vector<double> out_x;
    ofstream out1("sinfn.dat");

    in_abs_toll = 0.1;
    in_sd_toll = 0.71;  //analyse data initially

    for(y = 0; y < n; y++ )
    {
        in_data.push_back(sin(y*.1 ) + 1); 
    } 
    for(y = 0; y < n; y++ )
    {
        out1 << y << "    " << in_data[y]<< "\n";
    } 

    average = avg(in_data);
    cout << "\n"<< average << "\n\n";
    in_data_sd  = stddev(in_data, average);

    cout << "\n"<< in_data_sd << "\n\n";

    out_x = extreme(in_data, in_data_sd, i,i, in_abs_toll, in_sd_toll);
    
    for(y=0; y < out_x.size(); y++)
    {
        cout << y << "      "<< out_x[y]<< "\n";
    }
    return 0;

}



HEADER
3newhead.h
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
#ifndef Header
#define Header

#include <iostream>
#include <math.h>
#include <fstream>    
#include <vector>
#include <complex>

//shouldn't really be included here, but i don't know
//where else to put it...
using namespace std;

//function prototypes
double square(double  );

double avg(vector<double>);

double stddev(vector<double>, double );

vector<double> extreme( vector<double>, double, double,
        double , double, double);

#endif




FUNCTIONS
2extrema.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "3newhead.h"
using namespace std;

//square function (so i don't have to use pow!!)
double square(double x ){return x*x; }

//std_deviation
double stddev(vector<double> x, double avg)
{
    int i;
    double size, deviation, sum, var;
   /* sum = 0;
    for(i = 0; i < x.size(); i ++)
    {
        sum+= x[i];
    }
    avg = sum/x.size();
    */

    for(i = 0; i< x.size(); i++)
    {
        var += square(avg - x[i]);
    }
    deviation = sqrt(var/x.size());
    return deviation;
}

//average
//

double avg(vector<double> x)
{
    int i;
    double average, sum;
    sum = 0;

    for( i = 0; i < x.size(); i++)
    {  sum += x[i]; }

    average = sum/x.size();

    return average;
}





//picks out extrema ... kindof
vector<double> extreme( vector<double> in_data, double std_dev, double omega1,
        double field,double in_data_abs_toll,
        double in_data_sd_toll)
{
    int i;
    int flag_old_extrema;
    double out_flag_const_data;
    vector<double> out_extrema;

    //if its constant data then we can just send out one number 
    //(i reckon the average of the data is safe enough)

    if (std_dev < in_data_sd_toll)
    {
        out_flag_const_data = 1;
        cout << "\nconst_data\n";
        out_extrema.push_back(avg(in_data));
    }

    //if it isn't constant then we have loop over all the data
    else
    { 
        for(i= 0; i < in_data.size(); i++)
        {
            if ((in_data[i] > in_data[i-1]) && (in_data[i]>in_data[i+1])) 
            {
                if (out_extrema.size() == 0)
                {
                    //First extrema
                    out_extrema.push_back(in_data[i]);
                }

                // possible new extremum!!
                else
                {
                    flag_old_extrema = 0;

                    if ( abs(in_data[i] - out_extrema[i-1]) <= in_data_abs_toll ) 
                    { 
                        flag_old_extrema  = 1;
                    } 

                    if (flag_old_extrema  == 0)
                    {
                        out_extrema.push_back(in_data[i]);
                    }
                }
            } 

            /* 
Leaving this out so I can get the code to actually work properly 
... once it can find the maxima then it will be easy to get it to find the minima
     if ((in_data[i] < in_data[i-1]) && (in_data[i] < in_data[i+1])) 
                    {
                    if (out_extrema.size() == 0)
                    {
            //First extrema
            out_extrema.push_back(in_data[i]);
            }

            // possible new extremum!!
            else
            { 
            flag_old_extrema = 0; 
            if ( abs(in_data[i] - out_extrema[i-1]) <= in_data_abs_toll ) 
            { 
            flag_old_extrema  = 1;
            } 

            if (flag_old_extrema  == 0)
            {
            out_extrema.push_back(in_data[i]);
            }
            }
            }
            */     
        }
    }

    return out_extrema;
}





the code now runs .. which I am very happy about (thanks bazzy!) ...

but it doesn't seem to work particularly well... if I get it working properly I will post it here
Topic archived. No new replies allowed.