0

これが私の最初の投稿になりたくなかったのですが、ここで途方に暮れています。プログラムをコンパイルしようとすると、このエラーが発生し続けます (これは、単に長方形の面積と周囲を見つけることになっています)。これは私のヘッダー ファイルです。

#include <iostream>
using namespace std;

class Rectangle
{
public:
   Rectangle(float Lngth=1, float Wdth = 1);

   void setLngth(float Lngth);
   void setWdth(float Wdth);
   float getLngth(float Lngth);
    float getWdth(float Wdth);
    void Perimeter(float lngth, float wdth);
    void Area(float lngth, float wdth);
private:
    float Lngth;
    float Wdth;
};

これは私の .cpp ファイルです。

#include <iostream>
using namespace std;

#include "RealRectangle.h" // Employee class definition


 Rectangle::Rectangle(float Lngth, float Wdth)
 {
&Rectangle::setLngth;
&Rectangle::setWdth;
 }
 void Rectangle::setLngth(float Lngth)
 {
      if((Wdth > 0.0) && (Wdth < 20.0))
       float wdth = Wdth;
        else
            cout<<"Invalid Width."<<endl;
 }

 float Rectangle::getLngth(float Lngth)
 {
     return Lngth;
 }

void Rectangle::setWdth(float Wdth)
{
     if((Wdth > 0.0) && (Wdth < 20.0))
       float wdth = Wdth;
        else
            cout<<"Invalid Width."<<endl;
}

float Rectangle::getWdth(float Wdth)
{
    return Wdth;
}

void Rectangle::Perimeter(float lngth, float wdth)
    {
        cout<<"The Perimeter is "<<(2*(lngth + wdth));
    }
void Rectangle::Area(float lngth, float wdth)
    {
        cout<<"The Area is "<<(lngth * wdth);
    }

そして、これは私がエラーに遭遇し続ける場所です。コンパイラは、.cpp で行ったように、アンパサンドを追加してポインターを作成するように指示します。しかし、それはそれ自体で別のエラーを作成します。等々。何が間違っているのかわかりません。エラーは 10 行目と 11 行目で発生します。

#include <iostream>
using namespace std;

#include "RealRectangle.h" 


int main()
{
    Rectangle rectangle1();
    Rectangle rectangle2();

    cout<<rectangle1.Perimeter();
    cout<<rectangle2.Area();
}
4

3 に答える 3

2

最も厄介な解析と呼ばれるものに遭遇しました。

Rectangle rectangle1();
Rectangle rectangle2();

2 つのオブジェクトではなく、2 つの関数を宣言します。行う

Rectangle rectangle1;
Rectangle rectangle2;

さらに、おそらくそれら&Rectangle::setLngthを関数呼び出しに変更する必要があります。

于 2013-02-22T06:00:54.287 に答える
1

Rectangle :: Perimeter()とRectangle :: Area()はvoidタイプです。彼らは何も返しません。それでも、存在しない戻り値を使用して、それをに渡そうとしていcoutます。

これらの2つの関数を変更して、値を返すようにします。

float Rectangle::Perimeter(float lngth, float wdth)
{
        return 2 * (lngth + wdth);
}

float Rectangle::Area(float lngth, float wdth)
{
        return lngth * wdth;
}

または、main()関数を変更して、関数を呼び出すだけにします。これで、関数はすでに次のように出力されますcout

int main()
{
    Rectangle rectangle1;
    Rectangle rectangle2;

    rectangle1.Perimeter();
    rectangle2.Area();
}

しかし、まだ問題があります。これらの2つの関数は現在、長さと幅の引数を取りますが、それはあなたが望んでいることではないと思います。必要なのは、長方形オブジェクトの周囲と面積を取得することです。したがって、クラス変数を使用してそれを計算する必要があります。したがって、引数を省略し、代わりにプライベートデータメンバーを使用してください。

float Rectangle::Perimeter()
{
        return 2 * (Lngth + Wdth);
}

float Rectangle::Area()
{
        return Lngth * Wdth;
}

cppファイルの実装だけでなく、ヘッダーファイルのクラス宣言の関数シグネチャも更新することを忘れないでください。

さらに、コンストラクターは初期化作業を正しく委任しません。関数呼び出しは、function(arguments)ではなく、の形式&functionです。したがって、次のことを行う必要があります。

Rectangle::Rectangle(float Lngth, float Wdth)
{
    setLngth(Lngth);
    setWdth(Wdth);
}

最後に、Rectangleオブジェクトの宣言は関数プロトタイプとして誤って解釈されています。

Rectangle rectangle1();
Rectangle rectangle2();

コンパイラーは、とは引数をとらずにRectangleを返す関数であるrectangle1と考えています。rectangle2括弧は省略してください。

Rectangle rectangle1;
Rectangle rectangle2;

そして、私たちはまだ終わっていません(神様、このプログラムにはいくつのエラーがありますか:-P)。あなたsetLngthsetWdth関数が意図したとおりに機能していません:

void Rectangle::setLngth(float Lngth)
{
    if((Wdth > 0.0) && (Wdth < 20.0))
        float wdth = Wdth;
    else
        cout<<"Invalid Width."<<endl;
}

それをよく見てください。特に、関数の機能を示す行は、という名前の引数float wdth = Wdth;を取り、 (プライベート変数)が範囲内にあるかどうかを確認し、範囲内にある場合は、という新しいローカル変数を宣言して、と同じ値に設定します。floatLngthWdthfloatwdthWdth

この関数は、プライベート変数をまったく初期化しませんWdth。同じことがあなたのsetWdth関数にも当てはまります。それらも修正する必要があります。

于 2013-02-22T06:01:10.603 に答える
0

これ:

 Rectangle::Rectangle(float Lngth, float Wdth)
 {
&Rectangle::setLngth;
&Rectangle::setWdth;
 }

これである必要があります:

Rectangle::Rectangle(float Lngth, float Wdth)
{
    setLngth(Lngth);
    setWdth(Wdth);
}

この:

Rectangle rectangle1();
Rectangle rectangle2();

これである必要があります:

Rectangle rectangle1;
Rectangle rectangle2;
于 2013-02-22T06:01:25.020 に答える