gpt4 book ai didi

c++ - 从 TVector2 类 : "No matching function for call to Complex::Complex()" 派生复杂类

转载 作者:行者123 更新时间:2023-11-28 01:28:32 28 4
gpt4 key购买 nike

我想创建一个 C++ 类的复数。这里是Complex.h(最基本的形式)

#ifndef _COMPLEX
#define _COMPLEX

#include "TVector2.h"

class Complex : public TVector2{

public:
Complex(double re, double im): TVector2(re,im){;}

protected:
double m_re;
double m_im;
};

#endif

这是main.cpp

#include "Complex.h"
#include "TVector2.h"

int main() {

Complex c;
}

这是我正在使用的生成文件

CC         = g++ 
CFLAGS = -g -Wall
CFLAGSROOT = `root-config --cflags`
LIBSROOT = `root-config --glibs`

all: Main

Complex.o: Complex.cpp
$(CC) $(CFLAGS) -c Complex.cpp $(CFLAGSROOT)


Main: main.cpp Complex.o
$(CC) $(CFLAGS) -o Main main.cpp Complex.cpp $(CFLAGSROOT) $(LIBSROOT)

clean:
rm *.o

我收到这个错误:

main.cpp: In function ‘int main()’:
main.cpp:9:11: error: no matching function for call to ‘Complex::Complex()’
Complex c;
^

和这些注释:

In file included from main.cpp:2:0:
Complex.h:16:2: note: candidate: Complex::Complex(double, double)
Complex(double re, double im): TVector2(re,im), m_re(re), m_im(im){;}
^~~~~~~
Complex.h:16:2: note: candidate expects 2 arguments, 0 provided
Complex.h:13:7: note: candidate: Complex::Complex(const Complex&)
class Complex : public TVector2{
^~~~~~~
Complex.h:13:7: note: candidate expects 1 argument, 0 provided
Complex.h:13:7: note: candidate: Complex::Complex(Complex&&)
Complex.h:13:7: note: candidate expects 1 argument, 0 provided
Makefile:13: recipe for target 'Main' failed
make: *** [Main] Error 1

最佳答案

当编写 Complex c; 时,您正在尝试调用构造函数 Complex::Complex (),它在您的 Complex 类中未定义,因为唯一可用的构造函数有 2 个参数 Complex::Complex (double, double)

所以,有几种方法可以解决这样的问题:

  1. 只需提供构造函数参数:

    Complex c (0, 0);
  2. 如果提供构造函数参数看起来太庞大,您可以只提供一个默认构造函数,它对您有同样的作用:

    class Complex : public TVector2 {
    public:
    Complex (double re, double im): TVector2 (re, im) {}
    Complex (): TVector2 (0, 0) {}

    protected:
    double m_re;
    double m_im;
    };

    或者,如果您至少使用 C++-11:

    class Complex : public TVector2 {
    public:
    Complex (double re, double im): TVector2 (re, im) {}
    Complex (): Complex (0, 0) {}

    protected:
    double m_re;
    double m_im;
    };

    这将允许您编写 Complex c;

  3. 但是,如果您想要编写 Complex c (5);,除了 Complex 之外,它将对 im 部分进行 0 初始化c;,可以将这些构造函数参数标记为可选:

    class Complex : public TVector2 {
    public:
    Complex (double re = 0, double im = 0): TVector2 (re, im) {}

    protected:
    double m_re;
    double m_im;
    };

注意:我不同意从 TVector2 继承来表示复数的概念,因为继承表示“is-a”关系,而且在我看来复数不是 vector (这样的 vector 包含几种对复数没有意义的方法,并且缺少几种方法)。即使这样,您的 m_rem_im 成员也没有初始化,并且包含不确定的值,使用这样的值会导致未定义的行为。

考虑到这一点并进行更改,因此它不会从 TVector2 继承,代码片段将更改为:

(2):

class Complex {
public:
Complex (): m_re (0), m_im (0) {}
Complex (double re, double im): m_re (re), m_im (im) {}

protected:
double m_re;
double m_im;
};

或者(如果您至少使用 C++-11):

class Complex {
public:
Complex (double re, double im): m_re (re), m_im (im) {}
Complex (): Complex (0, 0) {}

protected:
double m_re;
double m_im;
};

(3) :

class Complex {
public:
Complex (double re = 0, double im = 0): m_re (re), m_im (im) {}

protected:
double m_re;
double m_im;
};

或者,除非此类代码用于学习目的,否则您可以只使用 std::complex .

关于c++ - 从 TVector2 类 : "No matching function for call to Complex::Complex()" 派生复杂类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52723748/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com