How to use results from different classes into one class by reference?

AI Thread Summary
The discussion centers on resolving syntax errors in a C++ program that integrates data from two classes, `series` and `function`, into a third class, `integral`. The user encounters multiple compilation errors related to class declarations and variable shadowing, particularly in the `do_integral` method. Suggestions include adding necessary `#include` directives for the `series` and `function` classes in the `integral` header file and avoiding variable name conflicts. The user attempts various modifications but continues to face similar issues, indicating a need for clearer variable management and proper initialization. The conversation highlights the importance of understanding class dependencies and variable scope in C++.
madtraveller
Messages
28
Reaction score
0

Homework Statement


I have:
- 1 class to read a data series (see text file attached) (series.h and series.cpp)
- 1 class for a function (function.h and function.cpp)
I'm trying to create another class which has input from 2 above class (as constant references) and produce output which has the same length as data series. Therefore, I create an object based on series class to store the result. But the program keep saying that the syntax wasn't correct despite I tried all methods I knew
Plz help me. Any suggestion would be appreciated.

Homework Equations



The Attempt at a Solution



* Main
Code:
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;

#include "function.h"
#include "series.h"
#include "integral.h"

// Main program
int main ()
{
    function function1;
    series in1;             // input data
    series out1;            // used to store result
    integral integral1;     // used input from series and function to generate output

    // read input data
    in1.read_data(string("data_test.txt"));
    in1.print_data();

    // test if function is correct
    function1.give_value( 5000.0 );

    // do integral from series and function to generate output
    integral1.do_integral( in1, function, &out1 );

    return 0;
}

* Series class:
- header file
Code:
#ifndef SERIES
#define SERIES

class series
{
    private:
        int length;                         // number of points
        double* data;
        struct Time_read                    // structure used to store time (format: xx/yy/zz)
        {
            unsigned int year, month, day;
        };
        vector<Time_read> time_vector;      // vector to store date

    public:
        series();
        bool read_data(const string &filename);
        void print_data();
};

#endif

- series.cpp
Code:
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;

#include "series.h"

// constructor initializes each data member to zero
series::series()
{
    length = 0;
    data = NULL;
}

// Read data from file
bool series::read_data(const string &filename)
{
    ifstream inputdata(filename.c_str());

    // check if file couldn't be opened
    if ( !inputdata )
    {
        cerr << "Error: file could not be opened" << endl;
        return false;
    }

    string dummy_string;    // temporary string used to read words
    float temp;

    // read and set length value
    inputdata >> dummy_string >> dummy_string >> dummy_string >> temp;
    length = temp;
    cout << "Length= " << length << endl;

    // read through "DATA" and "DATA" letters
    inputdata >> dummy_string >> dummy_string;

    data = new double [ length ];                   // allocate space for an array
    // Read date (xx/yy/zz)
    Time_read time_input;
    for (int i = 0; i < length; ++i)
    {
        inputdata >> setw(2) >> time_input.day;    // read 2 char and store them in time_input.day
        inputdata.ignore(1);                        // ignore 1 char. in this case "/" char
        inputdata >> setw(2) >> time_input.month;   // read 2 char and store in time_input.month
        inputdata.ignore(1);                        // ignore 1 char
        inputdata >> setw(2) >> time_input.year;     // read 2 char and store in time_input.year

        // store the reading result in time_vector
        time_vector.push_back(time_input);

        // read data
        inputdata >> data[i];
    }
    return true;
}

void series::print_data()
{
    cout << "--------------------------------" << endl;
    for (int i = 0; i < length; ++i)
    {
        cout << time_vector[i].day << "/" << time_vector[i].month << "/" << time_vector[i].year << " ";
        cout << setw(10) << data[i];
        cout << endl;   // new line at the end of each row
    }
    cout << "--------------------------------" << endl;
}

* function class
- header file
Code:
// prevent multiple inclusion of header file
#ifndef FUNCTION
#define FUNCTION

class function
{
    private:
        double trans;

    public:
        function();
        double give_value( const double &time );
};

#endif

- function.cpp
Code:
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;

#include "function.h"

// constructor initializes each data member to zero
function::function()
{
    trans = 0.0;
}

// Calculate function
double function::give_value( const double &time )
{
    const double   PI = 3.14159;
    double trans = 0.0;
    double diffuse = 0.0;
    double c = 2.0;
    double D = 10.0;
    double dx = 10000.0;

    trans = dx / ( 2.0 * sqrt( PI * D * pow(time, 3)));
    diffuse = dx - c * time;
    diffuse = exp ( - pow (diffuse, 2) / ( 4.0 * D * time ));
    trans *= diffuse;
    cout << setw(3) << time << setw(18) << trans << endl;
    return trans;
}

* Integral class
- header file
Code:
// Declaration  the integral class
// Member functions are defined in integral.cpp
// used input from series and function to generate output

// prevent multiple inclusion of header file
#ifndef INTEGRAL
#define INTEGRAL

class integral
{
    private:
        double* result;

    public:
        integral();
        void do_integral( const series &input, const function &func, double* result )
};

#endif

- integral.cpp
Code:
// used input from series and function to generate output

#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;

#include "integral.h"

integral::integral()
{
    result = NULL;
}

// Function for calculating convoluted integral
void integral::do_integral( const series &input, const function &func, double* result )
{
    const series &input;
    const function &func;
    series* result;

    // Loop for calculating integral
    for ( int i = 1; i < series.length; ++i )   // take length value from class series
    {
        for (int j = 0; j <= i; ++j )
        {
            result += series.data[i-j] * function.give_value[(3600*j)];
        }
    }
}

- sample data series
Number of points 18
DATE DATA
11/11/02 6.903
11/11/02 6.903
11/11/02 6.930
11/11/02 6.903
11/11/02 6.930
11/11/02 6.957
11/11/02 6.984
11/11/02 6.984
11/11/02 6.957
11/11/02 6.957
11/11/02 6.984
11/11/02 6.984
12/11/02 7.012
12/11/02 7.067
12/11/02 7.122
12/11/02 7.150
12/11/02 7.205
12/11/02 7.261

madtraveller
 
Physics news on Phys.org
madtraveller said:
But the program keep saying that the syntax wasn't correct despite I tried all methods I knew
Many compilers identify the line that is causing the syntax error. What output from the compiler do you get?
 
Mark44, thank for your reply
These are the errors: I'm using CodeBlock 10.05 with GCC compiler

integral.h|16|error: ISO C++ forbids declaration of 'series' with no type|
integral.h|16|error: expected ',' or '...' before '&' token|
integral.h|17|error: expected ';' before '}' token|
integral.h|17|error: expected ';' before '}' token|
integral.cpp|21|error: ISO C++ forbids declaration of 'series' with no type|
integral.cpp|21|error: expected ',' or '...' before '&' token|
integral.cpp|21|error: no 'void integral::do_integral(int)' member function declared in class 'integral'|

madtraveller
 
Your integral.h header is using classes defined in other headers (series and function), so when integral.cpp is compiled, the compiler doesn't know about the series and function classes. I might be wrong on this, but what I think you need to do is to add #include directives in integral.h for series.h and function.h.
 
Or you could just declare that series is a class in integral.h via a class series; statement. Doing that can help reduce the c++ problem of make wanting to rebuild everything from one tiny little change.
 
Hey,

I modified the program a little bit according to Mark44 suggestion:

- New integral.h
Code:
// Declaration  the integral class
// Member functions are defined in integral.cpp
// used input from series and function to generate output

// prevent multiple inclusion of header file
#ifndef INTEGRAL
#define INTEGRAL

#include "series.h"
#include "function.h"

class integral
{
    private:
        double* result;

    public:
        integral();
        void do_integral( const series &input, const function &func, double* result );
};

#endif

- New integral.cpp
Code:
// used input from series and function to generate output

#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;

#include "series.h"
#include "function.h"
#include "integral.h"

integral::integral()
{
    result = NULL;
}

// Function for calculating convoluted integral
void integral::do_integral( const series &input, const function &func, double* result )
{
    const series &input;
    const function &func;
    series* result;

    // Loop for calculating integral
    for ( int i = 1; i < series.length; ++i )   // take length value from class series
    {
        for (int j = 0; j <= i; ++j )
        {
            result[i] += series.data[i-j] * function.give_value[(3600*j)];
        }
    }
}

These are new errors:

\integral.cpp||In member function 'void integral::do_integral(const series&, const function&, double*)':|
\integral.cpp|25|error: declaration of 'const series& input' shadows a parameter|
\integral.cpp|25|error: 'input' declared as reference but not initialized|
\integral.cpp|26|error: declaration of 'const function& func' shadows a parameter|
\integral.cpp|26|error: 'func' declared as reference but not initialized|
\integral.cpp|27|error: declaration of 'series* result' shadows a parameter|
\integral.cpp|30|error: expected primary-expression before '.' token|
\integral.cpp|34|error: expected primary-expression before '.' token|
\integral.cpp|34|error: expected primary-expression before '.' token|
\integral.cpp|25|warning: unused variable 'input'|
\integral.cpp|26|warning: unused variable 'func'|

I also tried D H :
Code:
#ifndef INTEGRAL
#define INTEGRAL

class series;
class function;

class integral
{
    private:
        double* result;

    public:
        integral();
        void do_integral( const series &input, const function &func, double* result );
};

#endif

Code:
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;

//#include "series.h"
//#include "function.h"
#include "integral.h"

integral::integral()
{
    result = NULL;
}

- *.cpp
// Function for calculating convoluted integral
void integral::do_integral( const series &input, const function &func, double* result )
{
    const series &input;
    const function &func;
    series* result;

    // Loop for calculating integral
    for ( int i = 1; i < series.length; ++i )   // take length value from class series
    {
        for (int j = 0; j <= i; ++j )
        {
            result[i] += series.data[i-j] * function.give_value[(3600*j)];
        }
    }
}

And I received quite similar errors

\integral.cpp||In member function 'void integral::do_integral(const series&, const function&, double*)':|
\integral.cpp|26|error: declaration of 'const series& input' shadows a parameter|
\integral.cpp|26|error: 'input' declared as reference but not initialized|
\integral.cpp|27|error: declaration of 'const function& func' shadows a parameter|
\integral.cpp|27|error: 'func' declared as reference but not initialized|
\integral.cpp|28|error: declaration of 'series* result' shadows a parameter|
\integral.cpp|31|error: expected primary-expression before '.' token|
\integral.cpp|35|error: expected primary-expression before '.' token|
\integral.cpp|35|error: expected primary-expression before '.' token|
\integral.cpp|26|warning: unused variable 'input'|
\integral.cpp|27|warning: unused variable 'func'|

I was wondering if the declaration of the void function is correct or not. I also tried this statement in integral.h but still with no succedd

Code:
        void do_integral( const series &input, const function &func, [B]series* result[/B] );

madtraveller
 
In your do_integral function, you are declaring some variables twice, which is not good.
Code:
void integral::do_integral( const series &input, const function &func, double* result )
{
    const series &input;
    const function &func;
    series* result;

    // Loop for calculating integral
    for ( int i = 1; i < series.length; ++i )   // take length value from class series
    {
        for (int j = 0; j <= i; ++j )
        {
            result[i] += series.data[i-j] * function.give_value[(3600*j)];
        }
    }
}
input, func, and result are already declared as parameters, so you should not also declare them in the body of this function. Get rid of these lines of code, and that should eliminate at least some of your errors.
Code:
    const series &input;
    const function &func;
    series* result;
 
I modified the code.

- in integral.h

Code:
#include "series.h"
#include "function.h"

class integral
{
    private:
        double* result;

    public:
        integral();
        void do_integral( const series &input, const function &func, double* result );
};

- in integral.cpp
Code:
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;

//#include "series.h"
//#include "function.h"
#include "integral.h"

integral::integral()
{
    result = NULL;
}
void integral::do_integral( const series &input, const function &func, double* result )
{
    // Loop for calculating integral
    for ( int i = 1; i < series.length; ++i )   // take length value from class series
    {
        for (int j = 0; j <= i; ++j )
        {
            result[i] += series.data[i-j] * function.give_value[(3600*j)];
        }
    }
}

There're less errors but tbh I don't know why there're such errors:

\integral.cpp||In member function 'void integral::do_integral(const series&, const function&, double*)':|
\integral.cpp|31|error: expected primary-expression before '.' token|
\integral.cpp|35|error: expected primary-expression before '.' token|
\integral.cpp|35|error: expected primary-expression before '.' token|
 
The parameters are input and func, not series and function, which are classes.

In line 31 you have series.length. This should be input.length.
In line 35 you have series.data[...] and function.give_value[(3600*j)].
The first should be input.<something>, but it shouldn't be data, since that is a private member of the series class. From what I can see in your previously posted code, you don't have any public members that evaluate to a particular element in the data array.
The second should use func instead of function, but there's another problem as well. give_value is a function, but you are using it as if it were an array. func.give_value(3600*j) evaluates to a double.
 
  • #10
Hi again,

Thx Mark44 for your correction. Last week I worked on correcting all stupid mistakes and now my program can run properly.
- I had to create public functions to get the data instead of accessing a private member in series class;
- In integral.h
Code:
void do_integral( const series &input, const function &func, double* result );
+ result is a pointer to a class, at the beginning its length's not yet initialized correctly -> I had to give it a length identical to input
+ result is a pointer so this statement is not correct

Code:
result += series.data[i-j] * function.give_value[(3600*j)];

it should be
Code:
result -> series.get_data( i-j ) * function.give_value( 3600*j );

+ problem with "const series &input" and "const function &fun": C++ wanted these input for the function to be "const", therefore some public function used in series and function class have to be modified to smth like
Code:
double give_value( const double time ) const;

Code:
double get_data( int i ) const;

Thank you again for your help

madtraveller
 

Similar threads

Replies
2
Views
2K
Replies
6
Views
2K
Replies
9
Views
3K
Replies
75
Views
6K
Replies
3
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Back
Top