c++ - 为什么使用默认参数?

标签 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;
    }

最佳答案

您是指分配吗?

        *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) ]

关于c++ - 为什么使用默认参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23503181/

相关文章:

c++ - EOF 上的 std::ws 行为

c++ - 以最小的开销删除多余的参数包参数

c++ - 文本不会在屏幕上输出 C++/OpenGL

C++:如何正确地将 deque::front() 返回的变量传递出函数?

c++ - SQLite回调函数作为类成员

c++ - 使用可变参数模板将 vector 中的参数应用于函数时出现奇怪错误

c++ - Qt QPropertyAnimation for widgets in layout - widgets shaking

c++ - Winsock C++ 代理

c++ - 重载递归函数 C++

c++ - protected 成员是派生类中的 "not declared in this scope"