为什么要使用默认参数?

时间:2014-05-06 19:21:08

标签: c++

以下代码为我提供了默认值,即使我尝试了几个值。我找不到合乎逻辑的错误:(

我一定是对构造函数做错了,但我不知道为什么它们没有按预期工作。

    class cGeoPos {
    private:
        double lang = 8.7;
        double breit = 52.5;
    public:
        cGeoPos(){
        }
        cGeoPos(double lang_in, double breit_in){
            lang = lang_in;
            breit = breit_in;
        }
        void setGeoPos(double lang_in, double breit_in){
            lang = lang_in;
            breit = breit_in;
        }
        void printGeoPos(){
            cout << lang << "\t" << breit << endl;
        }
    };

    class cBaum {
    private:
        string art = "-";
        double hoch = 0;
        cGeoPos posi;
    public:
        cBaum(){
            art = "-";
            hoch = 0;
            posi.setGeoPos(0, 0);
        }
        cBaum(string art_in, double hoch_in, double lang_in, double breit_in)
        {
            art = art_in;
            hoch = hoch_in;
            cGeoPos;
            posi.setGeoPos(lang_in, breit_in);
        }
        int eingabe(){
            string art_in;
            double hoch_in, lang_in, breit_in;
            cout << endl << "Bitte Baumart eingeben: ";
            cin >> art_in;
            cout << endl << "Bitte Hoehe eingeben: ";
            cin >> hoch_in;
            cout << endl << "Bitte geografische Laenge eingeben: ";
            cin >> lang_in;
            cout << endl << "Bitte geografische Breite eingeben: ";
            cin >> breit_in;
            cBaum(art_in, hoch_in, lang_in, breit_in);
            if (art_in == "-") {
                return 0;
            }
            else {
                return 1;
            }
        }


    int main()
    {
        int i = 0;

        cBaum wald[1000];
        while (i < 1000) {
            if (wald[i].eingabe() == 0) {
                return 1;
            }
            i++;
        }

        return 0;
    }

2 个答案:

答案 0 :(得分:2)

您的意思是分配吗?

        *this = cBaum(art_in, hoch_in, lang_in, breit_in);

这里更常见的方法是说

this->art = art_in; 
this->hoch = hoch_in;

您还想添加输入验证。

更新以下是典型的C ++惯用语 Live On Coliru

#include <iostream>
#include <string>
#include <vector>

class cGeoPos {
    private:
        double lang;
        double breit;
    public:
        cGeoPos(double lang_in = 8.7, double breit_in = 52.5) 
            : lang(lang_in), breit(breit_in)
        { }

        friend std::ostream& operator<<(std::ostream& os, cGeoPos const& posi) {
            return os << "(" << posi.lang << ", " << posi.breit << ")";
        }

        friend std::istream& operator>>(std::istream& is, cGeoPos& posi) {
            return is >> posi.lang >> posi.breit;
        }
};

class cBaum {
    private:
        std::string art;
        double hoch;
        cGeoPos posi;
    public:
        cBaum(std::string art = "-", double hoch = 0, double lang = 0, double breit = 0)
            : art(art), hoch(hoch), posi(lang, breit)
        { }

        bool valid() const { return art != "-"; }

        friend std::ostream& operator<<(std::ostream& os, cBaum const& baum) {
            return os << "[ '" << baum.art << "', Hőhe:" << baum.hoch << ", posi:" << baum.posi << " ]";
        }

        friend std::istream& operator>>(std::istream& is, cBaum& baum) {
            if (is >> baum.art && baum.art != "-")
                return is >> baum.hoch >> baum.posi;
            else
                return is;
        }
};


int main()
{
    std::vector<cBaum> wald;

    do {
        std::cout << "Bitte (Art, Hőhe, G. Länge, G. Breite) angeben: ";

        cBaum baum;
        if (std::cin >> baum)
        {
            if (!baum.valid()) // "-" entered
                break;
            wald.push_back(baum);
        } else
        {
            if (std::cin.eof())
                break;
            std::cout << "Ungűltiger Eintrag\n";
            std::cin.clear();
            std::cin.ignore(1024, '\n');
        }
    } while (true);

    for (auto& baum : wald)
        std::cout << baum << std::endl;
}

输出

[ 'Eich', Hőhe:1, posi:(4678.3, 324.89) ]
[ 'Esp', Hőhe:89, posi:(-468.3, 3e+09) ]
[ 'Palm', Hőhe:5, posi:(4, 4) ]

答案 1 :(得分:0)

你可能期望你的代码

cBaum(art_in, hoch_in, lang_in, breit_in);

会调用构造函数。这不起作用。要让构造函数调用另一个构造函数,您必须使用专用语法,这在您的示例中不适用,因此请接受您不能调用构造函数。

改为使用初始化函数:

    initialize(string art_in, double hoch_in, double lang_in, double breit_in)
    {
        art = art_in;
        hoch = hoch_in;
        cGeoPos;
        posi.setGeoPos(lang_in, breit_in);
    }

然后,使用它而不是构造函数:

initialize(art_in, hoch_in, lang_in, breit_in);

为避免代码重复,请更改其他构造函数以调用此函数:

    cBaum(string art_in, double hoch_in, double lang_in, double breit_in)
    {
        initialize(art_in, hoch_in, lang_in, breit_in);
    }