Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ ';与call'不匹配;在声明后初始化对象时_C++_Oop - Fatal编程技术网

C++ ';与call'不匹配;在声明后初始化对象时

C++ ';与call'不匹配;在声明后初始化对象时,c++,oop,C++,Oop,下面的代码给了我 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: ... I

下面的代码给了我

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);
    ...
}

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

声明对象后,不能初始化它们
x()
尝试将
x
作为函数调用。

声明对象后,无法初始化它们
x()
尝试将
x
作为函数调用。

您尝试执行的操作仅在初始化时有效。 或者,您需要创建一个接受此类参数的成员函数

Integrator x; 
x(1.2) // Calling the constructor doesn't make sense here
您只能在初始化时直接调用构造函数(如前所述)


成员函数听起来像是一种方法。

您尝试执行的操作只适用于初始化。 或者,您需要创建一个接受此类参数的成员函数

Integrator x; 
x(1.2) // Calling the constructor doesn't make sense here
您只能在初始化时直接调用构造函数(如前所述)

成员函数听起来像是一种方法。

在定义对象之后,不能使用构造函数“初始化”对象。您可以做的是根据所需语法重写
操作符()
函数:

class Integrator : public Block {
    ...

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

    ...
};
定义对象后,不能使用构造函数“初始化”对象。您可以做的是根据所需语法重写
操作符()
函数:

class Integrator : public Block {
    ...

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

    ...
};

谢谢大家。在C++中我是新手,我有点困惑,所以我有时忘记基本原则。谢谢大家。对于C++中的OOP,我是新手,我有点困惑,所以我有时会忘记基本原理。虽然在实验上可能会这样做,但它会对代码< >操作程序()/<代码>的反直观使用,从而导致维护的头痛。我强烈建议改为使用命名成员函数。@Joachim Pileborg我尝试了您的示例(稍后我将更改为成员函数),并将错误更改为对
'Integrator::Integrator()'
的未定义引用,虽然从技术上讲可以这样做,但使用
操作符()
会非常违反直觉,导致维护方面的头痛。我强烈建议改为使用命名成员函数。@Joachim Pileborg我尝试了您的示例(稍后我将更改为成员函数),错误更改为对
'Integrator::Integrator()'
的未定义引用