简体   繁体   English

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

[英]C++ Calling a function from the header file in the main

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.我试图让 main.cpp 文件从 functions.h.cpp 文件中的函数类中读取函数。 I have a function defined as the following for the easy case:对于简单的情况,我有一个定义如下的函数:

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.cpp file: 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 IntegralCalculator which 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.如您所见,还有另一个名为IntegralCalculator类,它实际上进行IntegralCalculator的计算,尽管当函数定义包含在 main 中并且函数作为参数传递时,它应该没有错。 The functions.h file is defined as the following: 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.cpp file 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.我尝试在 main 中继承函数类,并且尝试为函数类创建命名空间并定义函数,但是这些都不起作用。 When I go to compile, I get the following error:当我去编译时,我收到以下错误:

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

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.h and functions.cpp files in the Makefile.编辑:如果你好奇,我在 Makefile 中包含了functions.hfunctions.cpp文件。 I was getting the same error when I was trying to put the square(double aX) definition in the IntegralCalculator class as well.当我尝试将square(double aX)定义也放入IntegralCalculator类时,我也遇到了同样的错误。

Your problem is, square is a member function AKA method.你的问题是, square是一个成员函数 AKA 方法。 In other words, there is no plain symbol square anywhere, it's functions::square .换句话说,任何地方都没有简单的符号square ,它是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 IntegralCalculator couldn't use even if you did.并且因为它不是静态方法,所以单独使用它是没有意义的,它总是需要一个相应的对象实例,而您没有,即使您有, IntegralCalculator也无法使用。

3 solutions, in the order of simplest, closest to your question, best: 3 个解决方案,按照最简单、最接近您的问题、最好的顺序:


From the constructor delcaration, move the square out 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 square in a namespace.最后,也许是最好的解决方案,将上述两者中最好的结合起来:将square放在命名空间中。 So functions.h becomes所以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);

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.没关系,但不会告诉其他 cpp 文件在您的 functions.cpp 文件中某处有一个名为 square 的全局函数。 What you need to do is include the header for that function so that the compiler knows that it exists:您需要做的是包含该函数的头文件,以便编译器知道它存在:

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这样更干净,更不容易混淆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM