在函数中使用函数
Using functions within functions
下面是我尝试 运行 的一段代码,其中我想要 运行 (dN
) 在我的主函数中 returns a 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;
};
虽然这段代码没有运行和returns错误:Call to non-static member function without an object argument
根据我的理解,C++ 不允许 nested functions 并且我上面的代码无法运行,因为我在主函数中调用了一个单独的函数。尽管(基于 link)它似乎确实可以通过在结构中定义一个函数来实现 "local classes",该函数必须在主函数中。虽然当我尝试这样做时:
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;
};
错误仍然存在。最终,我只是想在我的主函数中使用函数 dN
,即 returns 类型 complex<double>
的输出,但我不确定 best/operational 的实现方式。
我相信你误解了什么是嵌套函数。
嵌套函数如下所示:
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
}
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
有特定原因成为 class 的一部分:
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:
您可以使用 class 来完成此操作,如下所示
#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.);
}
下面是我尝试 运行 的一段代码,其中我想要 运行 (dN
) 在我的主函数中 returns a 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;
};
虽然这段代码没有运行和returns错误:Call to non-static member function without an object argument
根据我的理解,C++ 不允许 nested functions 并且我上面的代码无法运行,因为我在主函数中调用了一个单独的函数。尽管(基于 link)它似乎确实可以通过在结构中定义一个函数来实现 "local classes",该函数必须在主函数中。虽然当我尝试这样做时:
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;
};
错误仍然存在。最终,我只是想在我的主函数中使用函数 dN
,即 returns 类型 complex<double>
的输出,但我不确定 best/operational 的实现方式。
我相信你误解了什么是嵌套函数。 嵌套函数如下所示:
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
}
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
有特定原因成为 class 的一部分:
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: 您可以使用 class 来完成此操作,如下所示
#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.);
}