使用函数内的函数

时间:2017-01-14 07:23:44

标签: c++ function nested complex-numbers

下面是我试图运行的一段代码,我想在我的main函数中运行(dN)函数,它返回类型为complex<double>的值。

#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,上面的代码不起作用,因为我在main函数中调用了一个单独的函数。虽然(基于链接)确实可以通过在结构中定义一个必须在main函数内部的函数来实现“本地类”。虽然当我尝试这样做时:

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

错误仍然存​​在。最后,我只是想在我的main函数中使用返回类型dN的输出的函数complex<double>,但我不确定实现它的最佳/可操作方式。

3 个答案:

答案 0 :(得分:2)

我相信你误解了嵌套函数是什么。 嵌套函数如下所示:

int main()
{
    void nested() {} // not allowed in C++
}

您的问题的解决方案在编译器提供的错误消息中:

Call to non-static member function without an object argument

看看以下内容:

// 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
}

答案 1 :(得分:2)

  

最终,我只是想在我的main函数中使用返回类型为complex的输出的函数dN,但我不确定实现它的最佳/可操作方式。

使用免费功能,例如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.);
}

答案 2 :(得分: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.);
}