2

好的,所以我正在尝试使我的代码正常工作。它是一个带有“CArray”类的简单 C++ 程序。这个类有 2 个属性,数组大小和值。我希望主 C++ 程序创建 CArray 类的两个实例。在 CArray 类中,我有一个名为“AddArray(CArray)”的函数,它将另一个数组添加到当前数组中。我遇到的问题是我希望函数“AddArray”在 fortran 中添加两个数组。我知道,要复杂得多,但这就是我所需要的。我在类代码中链接两者时遇到问题。

#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 );
};

这里还有 CArray 函数文件。

#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();
}

这是我的 Fortran 代码。

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

如果有人可以帮助我设置将整数数组从 C++ 类发送到 fortran,我将不胜感激!

谢谢,

乔什·德里克

4

2 回答 2

0

You must realise that Val is already a pointer. So to call the subtraction routine, you would simply do this:

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

Please note that I don't know Fortran, so I can't comment on the Fortran part of code.


Additionally, there are tons of problems in your code.

  1. SetSize(int s) does total nonsense. It allocates s arrays of s size each, and leaks all but the last one. You probably just meant this:

    void CArray::SetSize( int s )
    {
        Size = s;
        delete[] Val;
        Val = new int[Size];
    }
    
  2. You're leaking memory all over the place. You should not manage memory manually, and simply use std::vector:

    class CArray
    {
      public:
        CArray();
    
        void    SetSize( size_t newSize );
        void    SetValues();
        void    GetArray();
    
        void    AddArray( const CArray &a );
        void    SubtractArray( const CArray &a );
        void    MultiplyArray( const CArray &a );
    
      private:
        std::vector<int> Val;
        int Size;
    };
    
    extern "C" 
    {
    void    Add( int*, int*, int*, int*);
    void    Subtract( int*, int*, int*, int*);
    void    Muliply( int*, int*, int *, int* );
    }
    

    With implementation like this:

    CArray::CArrary : Size(0)
    {}
    
    
    void CArray::SetSize( int newSize )
    {
      Val.resize(newSize);
      Size = newSize;
    }
    
    
    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; ++)
        {
          cout << Val[i] << " ";
        }
    }
    
    
    void CArray::SubtractArray( const CArray &a )
    {
    
        Subtract( &Val[0], &a.Val[0], &Size, &a.Size);
        GetArray();
    }
    
  3. GetArray() would be better renamed to PrintArray(), and ideally it should take a stream as parameter. SetValues() would be better named ReadValuesFromInput() or something similar.

  4. You should take parameters of type CArray by const reference instead of by value. I already did this in the code above.

  5. One function, one responsibility. SubtractArray() calling GetArray() is wrong.

于 2013-10-17T15:22:41.403 回答
0

现在我再次查看了您的 Fortran,我可以看出您对该语言的了解相当有限。

  1. END指定程序的结束,而不是循环/if 语句/模块的结束(它们分别是ENDDO/ END DOENDIF/END IFEND MODULE
  2. IMPLICIT NONE意味着必须声明所有变量,而您从未声明过i
  3. RETURN不是必需的,因为代码将在完成后返回
  4. 虽然没有必要,但您可能应该声明aasINTENT(INOUT)bas INTENT(IN)(虽然,诚然,我不确定这是否会破坏 C-Fortran 互操作性)。
  5. 你跳过了一个END(看起来像 for IF,考虑到你的缩进)

您的 Fortran 代码应该看起来更像

MODULE SubtractArrays
    USE ico_c_binding
    IMPLICIT NONE

  CONTAINS

    SUBROUTINE Subtract(a,b,s1,s2) bind(C,NAME='Subtract')
      INTEGER(INT_C), INTENT(IN) :: s1,s2
      INTEGER(INT_C), DIMENSION(s2), INTENT(IN) :: b
      INTEGER(INT_C), DIMENSION(s1), INTENT(INOUT) :: a
      INTEGER :: i

      IF(s1 == s2) THEN
         DO i=1,s1
            a(i) = a(i) - b(i)
         ENDDO
      ENDIF
    END SUBROUTINE
END MODULE
于 2013-10-17T15:51:36.197 回答