简体   繁体   中英

Enums in Constructors in C++

I am very new to Constructors and OOP in C++ and I encountered the following problem. I try to make the following class, but apparently something is wrong with the enumerators. Also I was wondering if I could somehow set EUR to be the default option.


class Amount
{
    // Todo 6.2
    // Implement class Amount
protected: 
    float Netto_;
    float Brutto_;
    enum tax_ { tax1 , tax2};
    enum Currency_ { EUR, USD }; 
    const float eur_to_usd = 1.13;
    const float usd_to_eur = 0.89;
    std::string Description_;

public:
    Amount(std::string Description , float Brutto, Currency_ Currency, tax_ taxtype) : Brutto_{Brutto} , Description_{Description}, Currency_{Currency}, tax_{taxtype} {} 

};

I am getting the following error:

"Currency_" is not a nonstatic data member or base class of class "Amount"

Thank you!

This line:

enum Currency_ { EUR, USD };

is not declaring a member variable, it is declaring a type. Since there's no variable, you cannot initialize the variable. You need to break it into two lines:

enum Currency_ { EUR, USD };
Currency_ Currency_val;

You need to declare a variable of the type Currency_ and make the enum definition public in order to to use it in the constructor. (and the same for tex_)

class Amount
{
public: 
    enum Currency_ { EUR, USD }; 
protected:
    Currency_ Currency;
    ...
};

void test() {
    Amount amount("description", 2.0, Amount::USD, Amount::tax1);
}

To declare a new enum type , you should do this:

enum MyEnumType {A, B, C};

Now you can declare instances of MyEnumType:

MyEnumType variable;

You can create the enum type inside a class, and you do. You never actually created an instance of that type, however.

You are confusing a type with a variable. You also have mixed capitalization for your names, which might contribute to your misunderstanding. Capitalization should not be random, but imply meaning. Often, types start with uppercase letters, member variables start with lowercase letters (and often end with a trailing _). Whatever actual code style you choose, be consistent . Don't declare some enums with uppercase enumerator names and other enums with lowercase enumerators, etc. Also, you should not mix constants, types, and variable declarations, but physically separate them into segments within your class. That can also help make it clearer what needs initialization and what does not.

Here's a minor rearrangement of your code, and fixing capitalization, and separation of type declarations and variable declarations:

#include <string>

class Amount
{
    // types (uppercase letter)
    enum Tax { TAX1, TAX2};
    enum Currency { EUR, USD }; 

    // variables (needing initialization.  All have trailing '_', not the types)
    std::string description_;
    float brutto_;
    float netto_;
    Currency currency_;
    Tax tax_;
    

public:
    Amount(std::string const & description, float brutto, Currency currency, Tax taxtype) :
        description_{description},
        brutto_{brutto}, 
        netto_{0},             
        currency_{currency}, 
        tax_{taxtype} 
    {} 
};

Hopefully this makes it clearer. Also, you should initialize the variables in the same order they are declared (since that's the order they're actually initialized anyway), and also you didn't initialize netto_ so I gave it a 0. And finally, I passed your string by reference to avoid unnecessary copies. I removed your constants because they were not part of this issue, and you probably want the conversion rates to be configurable anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM