C++ 在函数中使用函数
下面是我试图运行的一段代码,在我的主函数中有我想要运行的函数(C++ 在函数中使用函数,c++,function,nested,complex-numbers,C++,Function,Nested,Complex Numbers,下面是我试图运行的一段代码,在我的主函数中有我想要运行的函数(dN),它返回一个类型为complex的值 错误仍然存在。最终,我只是想在我的主函数中使用返回类型为complex的输出的函数dN,但我不确定实现这一点的最佳/操作方式 我相信您误解了嵌套函数的含义。 嵌套函数如下所示: int main() { void nested() {} // not allowed in C++ } 问题的解决方案在编译器提供的错误消息中: Call to non-static member fu
dN
),它返回一个类型为complex
的值
错误仍然存在。最终,我只是想在我的主函数中使用返回类型为
complex
的输出的函数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
}
我相信你误解了嵌套函数是什么。 嵌套函数如下所示:
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
}
最终,我只是想在我的主函数中使用返回complex类型输出的函数dN,但我不确定实现这一点的最佳/操作方式
使用自由函数,如main
is,除非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.);
}
复数dN(复数**N,整数k,整数i,复数kN,双T1)
{
...
}
int main(int argc,const char*argv[]{
...
//像这样
复合物dN_OUT=dN(NM,1,20,0,20.);
//不是这样的
//复合物dN_OUT=dN(**NM,1,20,0,20.);
}
最终,我只是想在我的主函数中使用返回complex类型输出的函数dN,但我不确定实现这一点的最佳/操作方式
使用自由函数,如main
is,除非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.);
}
复数dN(复数**N,整数k,整数i,复数kN,双T1)
{
...
}
int main(int argc,const char*argv[]{
...
//像这样
复合物dN_OUT=dN(NM,1,20,0,20.);
//不是这样的
//复合物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;
};
#包括
#包括
#包括
使用名称空间std;
常数复合物Im1(0.0,1.0)//虚数定义
类函数{
公众:
复数dN(复数**N,整数k,整数i,复数kN,双T1)
{
复数输出=Im1*(N[k][i]+kN)/(T1);
返回;
};
};
int main(int argc,const char*argv[]{
//…这里有更多代码
复数**NM=新复数*[1000];//1000x500数组
//运行循环进行初始化
对于(int i=0;i<1000;++i)
{
NM[i]=新络合物[500];
}
函数fun;//创建类实例
//调用函数注意此处的更改,即不正确通过**NM
复合dN_OUT=fun.dN(NM,1,20,0,20.);
返回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(复数**N,整数k,整数i,复数kN,双T1){
...
}
int main(int argc,const char*argv[]{
...
复合物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;
};
#包括
#包括
#包括
使用名称空间std;
常量复数Im1(0.0,1.0);//虚数定义
类函数{
公众:
复数dN(复数**N,整数k,整数i,复数kN,双T1)
{
复数输出=Im1*(N[k][i]+kN)/(T1);
返回;
};
};
int main(int argc,const char*argv[]{
//…这里有更多代码
复数**NM=新复数*[1000];//1000x500数组
//运行循环进行初始化
对于(int i=0;i<1000;++i)
{
NM[i]=新络合物[500];
}
函数fun;//创建类实例
//调用函数注意此处的更改,即不正确通过**NM
复合dN_OUT=fun.dN(NM,1,20,0,20.);
返回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(复数**N,整数k,整数i,复数kN,双T1){
...
}
int main(int argc,const char*argv[]{
...
复合物dN_OUT=dN(NM,1,20,0,20.);
}
某些编译器接受嵌套函数(作为其处理语言的扩展)这确实不是标准C++11的一部分。GCC接受C,不接受C++@BasileStrynekVitch,这是真的,但我不记得上次没有-pedantic
标志编译是什么时候了。@DeiDei啊,是的,对不起。我最初的尝试是像你说的那样使用嵌套函数,尽管我在wards(这是上面发布的代码)。感谢您的回答。虽然我目前正在测试代码,但它似乎有效,并将很快返回给您。我只是想澄清一下,为什么只添加静态工作?@Mathews24为什么只添加静态工作?我在回答中解释了。类型的非静态成员函数(结构或类)需要调用对象,而静态的-不需要。错误消息也符合要求。某些编译器接受嵌套函数(作为其处理语言的扩展)这确实不是标准C++11的一部分。GCC接受C,不接受C++@BasileStrynekVitch,这是真的,但我不记得上次没有-pedantic
标志编译是什么时候了。@DeiDei啊,是的,对不起。我最初的尝试是像你说的那样使用嵌套函数,尽管我在wards(这是上面发布的代码)。感谢您的回答。虽然我目前正在测试代码,但它似乎有效,并将很快返回给您。我只是想澄清一下,为什么只添加静态工作?@Mathews24为什么只添加静态工作?我在回答中解释了。类型的非静态成员函数(结构或类)需要调用对象,而静态对象-不需要。错误消息也符合。但是