2017-01-14 10 views
0

以下は、私が実行したいコードの部分です。complex<double>の値を返すメイン関数内で実行する関数があります(dN)。関数内の関数の使用

#include <iostream> 
#include <complex> 
#include <cmath> 
using namespace std; 

const complex<double> Im1(0.0,1.0); //imaginary number definition 

class Functions{ 
public: 
    complex<double> dN(complex<double> **N, int k, int i, complex<double> kN, double T1){ 
     complex<double> OUT = Im1*(N[k][i]+kN)/(T1); 
     return OUT; 
    }; 

}; 

int main(int argc, const char * argv[]) { 

    //...more code here 

    complex<int> **NM = new complex<int>*[1000]; //1000x500 array 
    //run loop to initialize 
    for (int i = 0; i < 1000; ++i) 
    { 
     NM[i] = new complex<int>[500]; 
    } 

    complex<double> dN_OUT = Functions::dN(**NM,1,20,0.,20.); 

    return 0; 
}; 

このコードを実行するとエラーを返しませんが:私の理解に基づいてCall to non-static member function without an object argument

、C++は、私は私の主な機能で別の関数を呼び出すためnested functionsと上記の私のコードは動作しないだろうことはできません。 。 (リンクに基づいて)表示されているように見えますが、メイン関数内になければならない構造体内の関数を定義することによって "ローカルクラス"を実装できます。私がそれをしようとすると、:

int main(int argc, const char * argv[]) { 

    complex<int> **NM = new complex<int>*[1000]; //1000x500 array 
    //run loop to initialize 
    for (int i = 0; i < 1000; ++i) 
    { 
     NM[i] = new complex<int>[500]; 
    } 

    struct Functions{ 
     complex<double> dN(complex<double> **N, int k, int i, complex<double> kN, double T1){ 
      complex<double> OUT = Im1*(N[k][i]+kN)/(T1); 
      return OUT; 
     }; 

    }; 

    complex<double> dN_OUT = Functions::dN(**NM,1,20,0.,20.); 

    return 0; 
}; 

エラーが続く。最終的には、私の主な機能のなかでタイプcomplex<double>の出力を返す関数dNを使いたいと思っていますが、これを実装するには最高の/操作上の方法が不明です。

答えて

2

入れ子関数が何であるか誤解していると思います。 Aネストされた関数は次のようになります。

int main() 
{ 
    void nested() {} // not allowed in C++ 
} 
あなたの問題を解決するには、コンパイラによって提供されるエラーメッセージにある

// Example 1 
struct Functions { 
    void func() {} 
}; 

int main() 
{ 
    // to call Functions::func() you would need to have an object 
    // of type Functions because Functions::func() is not a static function 
    Functions f; 
    f.func(); 
} 

// Example 2 
// by making func() static you can call it without an object: 
struct Functions { 
    static void func() {} 
}; 

int main() 
{ 
    Functions::func(); // OK 
} 

Call to non-static member function without an object argument 

次を見てみましょう

+0

一部の*コンパイラは、実際には標準C++ 11の一部ではないネストされた関数(処理する言語の拡張機能として)を受け入れます。 GCCはCのための[ネストされた関数](https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.htm)を受け入れます。 –

+0

@BasileStarynkevitchそれは本当ですが、私は最後の時間を思い出すことができません'-pedantic'フラグなしでコンパイルされます。 – DeiDei

+0

@DeiDei Ahhはい、申し訳ありません。最初の試みは、あなたが言ったように入れ子関数を使用することでしたが、後でmain関数の外のクラスに変更しました(これは上記のコードです)。あなたの答えをありがとうございます - それは動作しているように見えますが、私はこの時点でコードをテストしており、まもなくあなたに連絡します。単に静的な作業を追加するのはなぜですか? – Mathews24

2

Ultimately, I am simply wanting to use the function dN that returns output of type complex within my main function, but am unsure of the best/operational way to implement this.

mainのようなフリー関数を使用してください。dNは、クラスの一部であることを具体的な理由を持っています

complex<double> dN(complex<double> **N, int k, int i, complex<double> kN, double T1) 
{ 
... 
} 

int main(int argc, const char * argv[]) { 
    ... 
    //like this 
    complex<double> dN_OUT = dN(NM,1,20,0.,20.); 
    //not like this 
    //complex<double> dN_OUT = dN(**NM,1,20,0.,20.); 
} 
+0

しかし、「dN」への呼び出しに一致する関数がありません – Mathews24

+0

@ Mathews24、私はあなたが 'NM'の代わりに' ** NM'を使っているということを見落としました。元の質問とは正反対です。 – chris

1

オプション1:

#include <iostream> 
#include <complex> 
#include <cmath> 

using namespace std; 

const complex<double> Im1 (0.0, 1.0); //imaginary number definition 

class Functions { 
public: 
    complex<double> dN (complex<double> **N, int k, int i, complex<double> kN, double T1) 

    { 
     complex<double> OUT = Im1*(N[k][i] + kN)/(T1); 

     return OUT; 
    }; 

}; 

int main (int argc, const char * argv[]) { 

    //...more code here 

    complex<double> **NM = new complex<double>*[1000]; //1000x500 array 
               //run loop to initialize 
    for (int i = 0; i < 1000; ++i) 
    { 
     NM[i] = new complex<double>[500]; 
    } 

    Functions fun; //create class instance 

    //call the function NOTE the changes here i.e not correct passing **NM 
    complex<double> dN_OUT = fun.dN (NM, 1, 20, 0., 20.); 

    return 0; 
}; 

の下に他の人が言及したオプション2は、(変更を呼び出す代わりに、直接のようにあなたはクラスでこれを行うことができます** NMあなたはNMを使うべきです。

complex<double> dN(complex<double> **N, int k, int i, complex<double> kN, double T1){ 
    ... 
} 

int main(int argc, const char * argv[]) { 
    ... 
    complex<double> dN_OUT = dN(NM,1,20,0.,20.); 
} 
関連する問題