C++从main中的头文件调用一个函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15630293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:34:03  来源:igfitidea点击:

C++ Calling a function from the header file in the main

c++functionheaderscopeinheritance

提问by Cmertin

I'm writing an integral calculator. I'm trying to get the main.cpp file to read in the functions from the functions class which is in the functions.h.cpp files. I have a function defined as the following for the easy case:

我正在写一个积分计算器。我试图让 main.cpp 文件从 functions.h.cpp 文件中的函数类中读取函数。对于简单的情况,我有一个定义如下的函数:

double square(double aX)
 {
   return aX*aX;
 }

This function works when I include it in the main file, though it doesn't want to work when I try and call it from the main file when it's in the functions file. Here is the code that I have:

当我将它包含在主文件中时,这个函数可以工作,但当我尝试从主文件中调用它时,它不想工作,当它在函数文件中时。这是我拥有的代码:

Main.cppfile:

Main.cpp文件:

#include <iostream>
#include "IntegralCalculator.h"
#include "functions.h"

using namespace std;

int main()
  {
    IntegralCalculator Function(square);
    Function.print();
  }

As you can see, there is another class called IntegralCalculatorwhich actually does the calculation of the integral, though nothing should be wrong with that as it works when the function definition is included in the main, and the function is passed as a parameter. The functions.hfile is defined as the following:

正如您所看到的,还有另一个被调用的类IntegralCalculator,它实际上进行了积分的计算,尽管当函数定义包含在 main 中并且函数作为参数传递时,它应该没有问题。该functions.h文件定义如下:

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

#include <iostream>
#include <cmath>
#include "IntegralCalculator.h"


class functions : public IntegralCalculator
 {
  public:
   functions();
   double square(double aX);

 };

#endif

and the functions.cppfile is defined as such:

functions.cpp文件定义如下:

#include <iostream>
#include <cmath>
#include "functions.h"


double functions::square(double aX)
 {
   return aX*aX;
 }

I know that as of right now I don't need the <cmath>library, though I'm going to need it for more complicated functions when I add them.

我知道现在我不需要这个<cmath>库,尽管当我添加它们时我会需要它来处理更复杂的函数。

I have tried inheriting the functions class in the main, and I have tried creating a namespace for the functions class and defining the function, however none of these have worked. When I go to compile, I get the following error:

我尝试在 main 中继承函数类,并且尝试为函数类创建命名空间并定义函数,但是这些都不起作用。当我去编译时,我收到以下错误:

Main.cpp: In function ‘int main()':

Main.cpp: In function ‘int main()':

Main.cpp:9: error: ‘square' was not declared in this scope

Main.cpp:9: error: ‘square' was not declared in this scope

If someone could please help me figure out this problem. This code that I'm trying to write is more advanced than my class has gone over, so I'm not sure how to fix it, though I want to write the program this way.

如果有人可以请帮我解决这个问题。我正在尝试编写的这段代码比我的课程更高级,所以我不确定如何修复它,尽管我想以这种方式编写程序。

Edit: In case you were curious, I am including the functions.hand functions.cppfiles in the Makefile. I was getting the same error when I was trying to put the square(double aX)definition in the IntegralCalculatorclass as well.

编辑:如果您好奇,我将functions.hfunctions.cpp文件包含在 Makefile 中。当我尝试将square(double aX)定义也放入IntegralCalculator类中时,我也遇到了同样的错误。

回答by hyde

Your problem is, squareis a member function AKA method. In other words, there is no plain symbol squareanywhere, it's functions::square. And because it is not a static method, it makes no sense alone, it always needs a corresponding object instance, which you do not have and which IntegralCalculatorcouldn't use even if you did.

你的问题是,square是一个成员函数 AKA 方法。换句话说,square任何地方都没有简单的符号,它是functions::square. 并且因为它不是静态方法,所以单独使用是没有意义的,它总是需要一个相应的对象实例,您没有并且IntegralCalculator即使您拥有也无法使用。

3 solutions, in the order of simplest, closest to your question, best:

3 个解决方案,按照最简单、最接近您的问题、最好的顺序:



From the constructor delcaration, move the squareout of the class, make it a global function:

从构造函数声明中,将square类移出,使其成为全局函数:

In functions.h outside any class:

在任何类之外的functions.h中:

double square(double aX);

In functions.cpp

在functions.cpp中

double square(double aX) {
    return aX*aX;
}

Usage in main.cpp:

main.cpp 中的用法:

IntegralCalculator Function(square);


Or make it static method (which is how you would have to do if you used a language like Java, which didn't allow "naked" functions, but is not really C++ way):

或者让它成为静态方法(如果你使用像 Java 这样的语言,你必须这样做,它不允许“裸”函数,但不是真正的 C++ 方式):

in functions.h:

在functions.h中:

class functions // inheriting IntegralCalculator removed as not needed
{
    public:
    static double square(double aX);
};

and in functions.cpp:

在functions.cpp中:

//static
double functions::square(double aX) {
    return aX*aX;
}

Then you can pass it like this to `Function constructor in main.cpp:

然后你可以像这样将它传递给 main.cpp 中的 `Function 构造函数:

IntegralCalculator Function(functions::square);


Finally, perhaps the best solution, sort of combining the best of the two above: put squarein a namespace. So functions.h becomes

最后,也许是最好的解决方案,结合了上述两者中的优点:放入square命名空间。所以functions.h变成

namespace functions {
    double square(double aX)
}

functions.cpp becomes

函数.cpp变成

namespace functions {
    double square(double aX) {
        return aX*aX;
    }
}

You still use it in main like this:

你仍然像这样在 main 中使用它:

IntegralCalculator Function(functions::square);

回答by Radu Chivu

You have:

你有:

class functions : public IntegralCalculator
 {
  public:
   functions();
   double square(double aX);

 };

which is ok, but doesn't tell other cpp files that there is a global function called square somewhere in your functions.cpp file. What you need to do is include the header for that function so that the compiler knows that it exists:

没关系,但不会告诉其他 cpp 文件在您的 functions.cpp 文件中某处有一个名为 square 的全局函数。您需要做的是包含该函数的头文件,以便编译器知道它存在:

double square(double ax);
class functions : public IntegralCalculator
 {
  public:
   functions();
   double square(double aX);

 };

Also completely unrelated to your question, but worth mentioning after seeing your constructor code, you might want to use a typedef to declare the function pointer like so:

同样与您的问题完全无关,但在看到您的构造函数代码后值得一提的是,您可能希望使用 typedef 来声明函数指针,如下所示:

typedef double (IntegralFunctor*)(double);

So that your constructor can now be

这样你的构造函数现在就可以

IntegralCalculator::IntegralCalculator(IntegralFunctor func);

It's cleaner and less confusing this way

这样更干净,更不容易混淆