Hi,

I have a function which returns a pointer to array with data:
float * EZ_MATH::EZ_GE_BY_ME() { ... }

Now, how can I access this data? This doesnt work:

float * memA = temp_m.EZ_MATH::EZ_GE_BY_ME();
ofstream output("out_file", ios::out);
output << memA[0] <<endl;
output << memA[1] <<endl;

Help!

The of syntax temp_m.EZ_MATH::EZ_GE_BY_ME(); looks very strange, maybe you could post more of the program for context? For example:

  • what type is temp_m?
  • What is the code in the function EZ_MATH::EZ_GE_BY_ME?

Sure, below entire code:

sample_p6.h (which is included to the main file):

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
#include "sample_p5.h"



class EZ_MATH: public MATRIX
{

   public:      // public interfaces for this class

    EZ_MATH (char *, int); // example: EZ_MATH s("i_file", 3);
           // creates an object s from i_file with 3 equations

    EZ_MATH (int);

    float * EZ_GE_BY_ME();

    void EZ_GE_BY_MATLAB();

    void EZ_INVERT_BY_ME();

    void EZ_INVERT_BY_MATLAB();

   private:
    int n;  // 
    float AA[10][10];
    float B[10];
    char in_file[15];   //array to hold the input file name
};

EZ_MATH::EZ_MATH( int x)
    :MATRIX(x, x) // initialize base class MATRIX with dim1=x, dim2=x;
{

    n=x;
}

EZ_MATH::EZ_MATH(char * if_name, int x)
    :MATRIX(x, x) // initialize base class MATRIX with dim1=x, dim2=x;
{
    // your code for EZ_MATH constructor goes here
    int i,j;
    strcpy(in_file, if_name);
    ifstream input_file(in_file, ios::in);

    n = x;
    //read elements of AA and B from if_name

    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {
            input_file >> AA[i][j];
        }
    }

    for(i = 0; i < n; i++)
    {
        input_file >> B[i];
    }
    //output_file<<"+++ START OUTPUT FROM SAMPLE_P6.H:" << endl;
    //output_file<<"+++ MATRIX AA and B HAVE BEEN CREATED"<<endl;
    //output_file<<"+++ END OUTPUT FROM SAMPLE_P6.H:" << endl;
}

float * EZ_MATH::EZ_GE_BY_ME()
{
    int column, row, i, j, var;
    int end = 1;
    float temp;
    int z;

    for (column=0; column<n; column++)
    {
        if (AA[column][column] == 0)  //pivot in case diagonal element=0
        {
            int k;
            end = 0;
            for (k = column+1; (k<n && end==0); k++)
            {
                if (AA[k][column] != 0) //found non-zero diagonal element; swap lines
                {
                    temp = B[column];
                    B[column] = B[k];
                    B[k] = temp;
                    for (z=0; z<n; z++)
                    {
                        temp = AA[column][z];
                        AA[column][z] = AA[k][z];
                        AA[k][z] = temp;
                    }
                    end = 1;
                }
                else
                {}
            }
            if (end == 0) //singularity
            {
                //output_file<<"+++ START OUTPUT FROM SAMPLE_P6.H:" << endl;
                //output_file <<"+++ MY GAUSSIAN ELIMINATION SOLUTION:" <<endl;
                //output_file <<"+++ SYSTEM IS SINGULAR" <<endl;
                //output_file<<"+++ END OUTPUT FROM SAMPLE_P6.H:" << endl;
            }
            else
            {}
        }
        else
        {}

        for (row=column+1; row<n && end!=0; row++)
        {
            float multiplier;
            multiplier = (-AA[row][column])/(AA[column][column]);
            for (i=column; i<n; i++)
            {
                AA[row][i] += multiplier * AA[column][i];
            }
            B[row] += multiplier * B[column];
        }
    }
    float X[10];
    for (row=n-1; row>=0 && end!=0; row--)
    {
        float accumulation=0;
        for (var=n-1; var>row; var--)
        {
            accumulation += AA[row][var] * X[var];
        }
        X[row] = (B[row] - accumulation)/(AA[row][row]);
    }

    if (end != 0)
    {
        output_file<<"+++ START OUTPUT FROM SAMPLE_P6.H:" << endl;
        output_file <<"+++ MY GAUSSIAN ELIMINATION SOLUTION:" <<endl;
        for (i=0; i<n; i++)
        {
        output_file <<"+++ X["<<i<<"]="<<setprecision(2) <<setiosflags(ios::fixed | ios::showpoint) <<X[i] <<endl;
        }
        output_file<<"+++ END OUTPUT FROM SAMPLE_P6.H:" << endl;
    }
    return X;
}

Main program:

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>

#include "sample_p6.h"

class EZ_CURVE_FIT: public EZ_MATH
{
   public:
    EZ_CURVE_FIT(int); // constructor; 
        // example: s.EZ_CURVE_FIT(5);
        // there are 5 pairs of data points;

    void LS_FIT_BY_ME(char *); 
        // example: s.LS_FIT_BY_ME("data_file");
        // perform least squares fit using inheritance 
        // data is in "data_file";

    void LS_FIT_BY_MATLAB (char *); 
        // example: s.LS_FIT_BY_MATLAB("data_file);
        // perform least squares fit using MATLAB; 

   private:
    int np; // number of data pairs
};

// constructor code 
EZ_CURVE_FIT::EZ_CURVE_FIT(int x)
    :EZ_MATH(2) // call base class constructor, (we need to solve 
       // a linear equation system of AX=B, Dimension of A is 2X2) ;
{
        np = x;
}

void EZ_CURVE_FIT::LS_FIT_BY_ME(char * data_file)
{
    float S1, S2, S3, S4, S5, S6, error;
    error=S1=S2=S3=S4=S5=S6=0.0;
    float X[15], Y[15];
    int i, j;


    float m, b;     
    ifstream input_file(data_file, ios::in);   

     for(i=0; i<np; i++)
        {
            input_file >> X[i]>> Y[i];
        }

    // calculate the S1-S6 values
    for (i=0;i<np;i++)
    {
        S1 += (X[i]*X[i]);
        S2 += (X[i]);
        S3 += (X[i]*Y[i]);
        S4 += X[i];
        S5 += 1;
        S6 += Y[i];
    }


    // put them into a S_file in the format that sample_p6.h
    // will read them:

    ofstream out_s_file("S_file", ios::out);

    out_s_file << S1 << " " << S2 << endl;
    out_s_file << S4 << " " << S5 << endl;
    out_s_file << S3 << endl;
    out_s_file << S6 << endl;

    // declare an EZ_MATH object:

    EZ_MATH temp_m(2);

    // access EZ_GE_BY_MATLAB
    float * memA = temp_m.EZ_MATH::EZ_GE_BY_ME();



    ifstream in_out_61("out_61", ios::in);

    ofstream output("out_71", ios::out);

    output << " $$ " << memA[0] <<endl;
    output << " $$ " << memA[1] <<endl;   
    output << " $$ " << memA[2] <<endl;


}

EZ_MATH::EZ_GE_BY_ME returns a pointer to X, but X is a local variable to the function. When you return the pointer points to a memory location that is not valid after the function exits.

To make this work in the way that you intended, you will either have to new X inside EZ_MATH::EZ_GE_BY_ME or pass a pointer to it into the function:

float* EZ_MATH::EZ_GE_BY_ME()
{
    /* Do things */

    float* X = new float[ N ];

    /* DO things with X */

    return X;
}

Also, when you call EZ_MATH::EZ_GE_BY_ME on line 79 of your main file, you should do it like this:

float * memA = temp_m.EZ_GE_BY_ME();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.