nullReferenceException mathnetマトリックスタイプでテストするとき(m == nullptr)
-
27-10-2019 - |
質問
私は、マトリックス型から派生した型の属性を比較しようとすると発生する奇妙なnullReferenceExceptionに苦労しています。 Mathnetライブラリ nullptrへ。
Mathnet :: numerics :: linearAlgebra :: Matrixから派生したクラス変換を備えたAA C ++/CLIクラスライブラリを書きたいと思います。他の位置に比べて位置を設定できるようになりたいので、私は属性を持っています Transformation^ parent
. 。に if(parent == nullptr){ ... }
現在の変換が親を持っているかどうかをテストしたいのですが、この例外は if(parent == nullptr)
:
An unhandled exception of type 'System.NullReferenceException' occurred in MathNet.Iridium.dll
Additional information: Object reference not set to an instance of an object.
私の変革のクラスは次のように見えます:
/// Transformation.h
using namespace MathNet::Numerics::LinearAlgebra;
using namespace System;
ref class Transformation : Matrix
//ref class Transformation : A
{
public:
Transformation(void);
Transformation^ parent;
void DoSomething();
};
/// Transformation.cpp
#include "StdAfx.h"
#include "Transformation.h"
Transformation::Transformation(void) : Matrix(4,4)
{
}
void Transformation::DoSomething()
{
if(parent == nullptr) // Produces NullReferenceException
{
Console::WriteLine("parent is nullptr");
}
Matrix^ m;
if(m == nullptr) // Produces NullReferenceException, too
{
Console::WriteLine("m is nullptr");
}
}
実際にnullであるマトリックスタイプの変数をnullptrと比較すると、この例外がスローされているようです。適切に初期化されている場合、例外はないため、これは正常に機能します。
Matrix^ m = gcnew Matrix(4,4);
if(m == nullptr) // works fine
{
Console::WriteLine("");
}
別のクラスから変換を導き出すとき、 ref class Transformation : A
それ以外の ref class Transformation : Matrix
, 、すべてが正常に機能します。
そして今、それは本当に奇妙になります。クラスライブラリをC#アプリケーションで使用したかったのです。電話 t.DoSomething()
変換では、tはnullReferenceExceptionをスローします。ただし、C#アプリケーションに直接ヌルテストを含めると、機能します。
Transformation t = new Transformation();
// t.DoSomething(); // Throws NullReferenceException
if (t.parent == null) // OK!
{
Console.WriteLine("parent is null");
}
C ++/CLIアプリケーションで同じことを行うと、nullReferenceExceptionが再びスローされます。
Transformation^ t = gcnew Transformation();
// t->DoSomething(); // Throws NullReferenceException
if(t->parent == nullptr) // Throws NullReferenceException
{
Console::WriteLine("parent is nullptr");
}
これがどこから来るのかという提案はありますか?私は本当にかなり困惑しています...
解決
少なくとも==演算子がヌルの参照をスローする方法で実装される可能性があります。
Object :: ReferenceEquals(obj、null)を呼び出して、それが機能するかどうかを確認できますか?