C++ class functions calling fortran subroutine

Posted by user2863626 on Stack Overflow See other posts from Stack Overflow or by user2863626
Published on 2013-10-17T15:06:44Z Indexed on 2013/10/17 15:55 UTC
Read the original article Hit count: 182

Filed under:
|
|

Okay so I am trying to make my code work. It is a simple C++ program with a class "CArray". This class has 2 properties, the array size, and the value. I want the main C++ program to create two instances of the class CArray. In the class CArray, I have a function called "AddArray( CArray )" where it adds another array to the current array. The problem I am stuck with, is that I want the function "AddArray" to add the two arrays in fortran. I know, much more complicated, but that is what I need. I am having issues with linking the two inside the class code.

#include <iostream>
using namespace std;

class CArray
{
    public:
    CArray();
    ~CArray();
    int Size;
    int*    Val;

    void    SetSize( int );
    void    SetValues();
    void    GetArray();


    extern "C" 
    {
    void    Add( int*, int*, int*, int*);
    void    Subtract( int*, int*, int*, int*);
    void    Muliply( int*, int*, int *, int* );
    }
    void    AddArray( CArray );
    void    SubtractArray( CArray );
    void    MultiplyArray( CArray );
};

Also here is the CArray function file.

#include "Array.h"

#include <iostream>
using namespace std;

CArray::CArray()
{
}


CArray::~CArray()
{
}


void CArray::SetSize( int s )
{
    Size = s;
    for ( int i=0; i<s; i++ )
    {
    Val = new int[Size];
    }
}


void CArray::SetValues()
{
    for ( int i=0; i<Size; i++ )
    {
    cout << "Element " << i+1 << ": ";
    cin >> Val[i];
    }
}


void CArray::GetArray()
{
    for ( int i=0; i<Size; i++ )
    {
    cout << Val[i] << " ";
    }
}


void CArray::AddArray( CArray a )
{
    if ( Size == a.Size )
    {
    Add(&Val, &a.Val);
    }
    else
    {
    cout << "Array dimensions do not agree!" << endl;
    }
}


void CArray::SubtractArray( CArray a )
{
    Subtract( &Val, &a, &Size, &a.Size);
    GetArray();
}

Here is my Fortran code.

module SubtractArrays
    use ico_c_binding
    implicit none

    contains

    subroutine Subtract(a,b,s1,s2) bind(c,name='Subtract')
    integer s1,s2
    integer a(s1),b(s2)

    if ( s1.eq.s2 )
        do i=1,s1
        a(i) = a(i) - b(i)
        end
    return 
    end
end

If someone could just help me with setting me up to send arrays of integers from C++ classes to fortran I would greatly appreciate it!

Thank you,

Josh Derrick

© Stack Overflow or respective owner

Related posts about c++

Related posts about linux