在函数中使用函数

Using functions within functions

本文关键字:函数      更新时间:2023-10-16

下面是一段我试图运行的代码,我在我的主函数中有我想运行的函数(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++不允许嵌套函数,我上面的代码不起作用,因为我在我的主函数中调用了一个单独的函数。虽然(基于链接)看起来确实可以通过在结构中定义一个函数来实现"局部类",该函数必须在主函数内部。虽然当我尝试这样做时:

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 函数中使用返回类型 complex<double> 输出的函数dN,但不确定实现这一点的最佳/操作方法。

我相信

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

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
}

最终,我只是想在我的主函数中使用返回复杂类型输出的函数 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.);
}

选项 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.);
}