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

In summary: time_input.year, time_input.month, time_input.day); }} else { // if file was opened successfully, read and set length value inputdata >> dummy_string; // read string and set length value to it length = dummy_string.size(); // set length value to string size}}return true;}// function to generate output series::function(){ // global variables // input data // output data // integral // function call
  • #1
madtraveller
28
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
  • #2
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?
 
  • #3
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
 
  • #4
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.
 
  • #5
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.
 
  • #6
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
 
  • #7
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;
 
  • #8
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|
 
  • #9
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
 

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

1. How can I use results from different classes in one class by reference?

One way to use results from different classes in one class is by using the "extends" keyword. This allows a class to inherit the properties and methods of another class, making it possible to use results from the parent class in the child class.

2. Can I use results from multiple classes in one class without inheritance?

Yes, it is possible to use results from multiple classes in one class without inheritance. This can be done by creating objects of the different classes and using their properties and methods directly in the new class.

3. Is it necessary to use the "extends" keyword to use results from different classes in one class?

No, it is not necessary to use the "extends" keyword to use results from different classes in one class. It is just one way to achieve this, but there are other methods such as creating objects or using interfaces.

4. Can I use results from classes with different data types in one class?

Yes, it is possible to use results from classes with different data types in one class. This can be done by converting the data types to a common type or by using interfaces to handle different data types.

5. How do I ensure that the results from different classes are used correctly in one class?

To ensure that the results from different classes are used correctly in one class, it is important to have a clear understanding of the properties and methods of each class and how they can be used together. Proper error handling and testing can also help identify any issues with using results from different classes in one class.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
979
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
5
Views
864
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Back
Top