0

以下代码给了我

test2.cc:248:14: error: no match for call to '(Integrator) (Input, double)'
test2.cc:249:11: error: no match for call to '(Integrator) (Integrator&, double)'

关于编译。

class Integrator : public Block {
    private:
            ... 
        Input input;    
        double init_value;              
    public:
        Integrator();
        Integrator(Input i, double initval = 0) : input(i), init_value(initval) {}
        Integrator(Integrator &i, double initval = 0) : input(i), init_value(initval) {}
...
};

// + is overloaded
Input operator + (Input a, Input b) { return new Add(a,b); }

int main() {
    Constant a(4.0); // Input
    Integrator x,y;
    ...
    x(y + a, 0.0); // + is overloaded for Inputs
    y(x, -2.0);
    ...
}

我只发布代码片段,因为这是我的作业。如果这些还不够,我可以添加更多。我看到类似的代码在工作,所以我尝试使用它(进行了一些编辑),但它对我不起作用......

4

3 回答 3

2

You can't initialize objects after they're declared. x() tries to call x as a function.

于 2012-12-04T12:47:11.027 回答
2

What you are trying to do would only work on initialization. Or You need to create a member function taking such arguments.

Integrator x; 
x(1.2) // Calling the constructor doesn't make sense here

You can only call the constructor directly on initialization (as already said).

Integrator x(1.2) ;

A member function sounds like the way to go.

于 2012-12-04T12:47:29.577 回答
2

在定义对象后,您不能使用构造函数“初始化”对象。您可以做的是覆盖operator()您想要的语法的函数:

class Integrator : public Block {
    ...

public:
    void operator()(Input i, double initval = 0)
        {
            input = i;
            init_value = initval;
        }

    ...
};
于 2012-12-04T12:48:48.967 回答