web-dev-qa-db-ja.com

C ++エラー:の初期化に一致するコンストラクターがありません

あるオブジェクトの値を同じクラスの別のオブジェクトに設定できるC++でのメンバーごとの割り当ての実践。このプログラムのアイデアは、長方形オブジェクトをいくつかの値で初期化し、別の長方形オブジェクトを作成しますが、最初のオブジェクトの値を2番目のオブジェクトに割り当てます。

それは私にエラーを与えます、それは以下に投稿されています、そしてそれが何であるか私には理解できません、そしてそれは私を笑わせています笑

これは私の四角形です。

#ifndef RECTANGLE_H
#define RECTANGLE_H

class Rectangle {
    private:
        double length;
        double width;
    public:
        Rectangle(double, double);
        double getLength() const;
        double getWidth() const;
};

Rectangle::Rectangle(double l, double w) {
    length = l;
    width = w;
}

double Rectangle::getWidth() const { return width; }
double Rectangle::getLength() const { return length; }

#endif

これは私のRectangle.cppです

#include <iostream>
#include "rectangle.h"
using namespace std;

int main()
{
    Rectangle box1(10.0, 10.0);
    Rectangle box2;

    cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
    cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;

    box2 = box1;

    cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
    cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;

    return 0;
}

これはコンパイル時のエラーです。

skipper~/Desktop/Programming/Memberwise: g++ rectangle.cpp 
rectangle.cpp:7:12: error: no matching constructor for initialization of
      'Rectangle'
        Rectangle box1(10.0, 10.0);
                  ^    ~~~~~~~~~~
./rectangle.h:4:7: note: candidate constructor (the implicit copy constructor)
      not viable: requires 1 argument, but 2 were provided
class Rectangle {
  ^
./rectangle.h:4:7: note: candidate constructor
      (the implicit default constructor) not viable: requires 0 arguments, but 2
      were provided
1 error generated.

編集:これは私がそれを機能させることができた方法です。すべてをrectangle.cppに移動し、コンストラクターにデフォルトの引数を与えました。

編集されたrectangle.cpp

#include <iostream>
using namespace std;

class Rectangle {
     private:
         double length;
         double width;
    public:
        //Rectangle();
        Rectangle(double = 0.0, double = 0.0);
        double getLength() const;
        double getWidth() const;
 };

 int main()
 {
    Rectangle box1(10.0, 10.0);
    Rectangle box2;

    cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
    cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;

    box2 = box1;

    cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
    cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;

    return 0;
}

Rectangle::Rectangle(double l, double w) {
    length = l;
    width = w;
}

double Rectangle::getWidth() const { return width; }
double Rectangle::getLength() const { return length; }

私が行った唯一の変更は、ユーザー定義コンストラクターにデフォルトの引数を与えることでした。ただし、変更がrectangle.hにある場合は機能しませんでした。ただし、クラスとメンバー関数の定義をrectangle.cppに移動すると、機能しました。そのため、プログラムを機能させましたが、実際の問題には対処しませんでした。これは、クラスとメンバー関数の定義がrectangle.hにある場合、コンパイルされません。

誰かがこの問題に直面し、これに対する解決策を見つけた場合は、それをどのように行ったかをお知らせください。ありがとう:)

6
skipper

ラインで

Rectangle box2; // no default constructor, error

Rectangleのデフォルトのコンストラクターを呼び出そうとしています。 Rectangleには2つのパラメーターを受け取るユーザー定義のコンストラクターがあるため、コンパイラーはこのようなデフォルトのコンストラクターを生成しません。したがって、次のようにパラメータを指定する必要があります

Rectangle box2(0,10);

コードのコンパイル時に発生するエラーは次のとおりです。

Rectangle.cpp:8:15:エラー: 'Rectangle :: Rectangle()'への呼び出しに一致する関数がありませんRectangle box2;

解決策は、Rectangleのデフォルトコンストラクターを作成することです。これは、ユーザー定義のコンストラクターのために自動的に生成されないためです。

Rectangle(); // in Rectangle.h

Rectangle::Rectangle(){} // in Rectangle.cpp (or Rectangle::Rectangle() = default; in C++11)

別の解決策(データが初期化されないままにならないため、望ましい解決策)は、既存のコンストラクターにデフォルトの引数を割り当てることです。

Rectangle::Rectangle(double l = 0, double w = 0); // only in Rectangle.h

このようにして、クラスをDefault-Constructibleにします。

6
vsoftco

コンパイラーが生成したデフォルトのコンストラクターは、コンストラクターが定義されていない場合にのみ生成されます。コンストラクターを定義するため、デフォルトのコンストラクターが必要な場合は、自分で提供する必要があります。おそらく最も簡単な(おそらく)2つの引数のコンストラクターでデフォルトの引数を使用してそれを提供することです:

Rectangle(double l=0, double w=0)

また、以下に示すようにinlineキーワードを使用する必要があります。そうしないと、リンカーエラーが発生する場合があります。

inline Rectangle::Rectangle(double l, double w) {
    length = l;
    width = w;
}

inline double Rectangle::getWidth() const { return width; }
inline double Rectangle::getLength() const { return length; }
0
Steve