0

下面是我遇到的情况。我需要调用一个类(ManipFunction)中包含的方法(即manip_A()),主函数中提供了一些参数。这些参数是变量(一些双精度)和一个函数(即 func)。有人可以帮忙吗?谢谢你。

// manip.hpp
class ManipFunction
{
// for example ..
private:
    // Initialization function ...

    // Copy constructors ...

    // Core functions ...
    double manip_A();
    double manip_B();


public:
    // Public member data ...
    ...
    // Constructors ...
    ...
    // Destructors ...
    ...
    // Assignment operator ...
};

.

// manip.cpp
#include"manip.hpp"

// Core functions
double ManipFunction::manip_A() const
{
    // Apply manip_A() to the function and parameters provided in manip_test.cpp
}

double ManipFunction::manip_B() const
{
    // Apply manip_B() to the function and parameters provided in manip_test.cpp
}

// Initialisation
...
// Copy constuctor
...
// Destructor
...
// Deep copy
...
}

.

// manip_test.cpp

#include<iostream>
// Other required system includes

int main()
{
    double varA = 1.0;
    double VarB = 2.5;

    double func (double x) {return x * x};

    double manip_A_Test = ManipFunction::manip_A(func, varA, VarB);

    std::cout << "Result of Manip_A over function func: " << manip_A_Test <<  endl;

        return 0;
}
4

1 回答 1

2

OK 这里有几个误区。

1) 函数内部的函数不是合法的 C++。

int main()
{
    ...
    double func (double x) {return x * x};
    ...
}

这是不允许的。移出func主线。尾随;也不合法。

2) 要调用 ManipFunction 方法,您需要一个 ManipFunction对象。您的代码没有提供。

int main()
{
    ...
    ManipFunction mf; // a ManipFunction object
    mf.manip_A(func, varA, VarB); // call the manip_A method on the ManipFunction object
    ...
}

3)虽然你说你想要manip_A有参数,但你还没有声明任何参数。这里我给出了manip_A两个参数,都是 type double

class ManipFunction
{
    double manip_A(double x, double y);
    ...
};

4)虽然你说你想manip_A从 main 内部调用,但在你的代码中你已经声明manip_Aprivate. public如果您想直接从 main 调用它,则必须如此。

最后,我只想说发布您的真实代码可能更好,而不是我认为您发布的虚构代码。

希望这可以帮助

于 2012-11-09T20:56:34.817 回答