简体   繁体   English

在构造函数C中使用枚举

[英]use of enum in a constructor c++

I'm trying to create a class "Apple" that has two values 1. int n 2. enum color 我正在尝试创建一个具有两个值的“ Apple”类1. int n 2.枚举颜色

but my code doesnt work and I get an "No matching constuctor for Initialization" Error 但是我的代码不起作用,并且出现“初始化时没有匹配的构造函数”错误

I don't know what is the best way to do it. 我不知道什么是最好的方法。

#include <iostream>
#include<stdexcept>

using namespace std;

class Color{
public:
   enum color{r,g};
};

class Apple: public Color {
    int n;
    Color c;
public:
    Apple(int n,Color color){
        if(n<0)throw runtime_error("");
        this->n=n;
        this->c=color;
    }
    int n_return(){return n;}
};
int main(){
    try{
        const Apple a1{10,Color::g};
        cout << a1.n_return();}
    catch(runtime_error&){
        cout<<"ER\n";
    }
    return 0;
}

I don't want to change anything in main. 我不想更改任何主要内容。

Additionally, how can I set the default color of apple to g in the constructor for when there is no color given? 另外,当没有颜色时,如何在构造函数中将apple的默认颜色设置为g?

Your Color c; 您的Color c; member in class Apple is referring to the Color base class instead of the color enumerator defined inside it. class Apple成员是指Color基类,而不是其内部定义的color枚举器。 Having said that, from a design perspective, you seem to be inheriting an Apple from a Color . 话虽如此,从设计的角度来看, 您似乎是从Color 继承了Apple I believe what you intended for is having each Apple instance hold a color value. 我相信您打算让每个Apple实例都拥有一个颜色值。 For that, you want composition , not inheritance -- as an Apple is not a color , it is-a fruit : ) that has-a color. 为此,您需要组成而不是继承 - 苹果不是一种颜色 ,而是-一种水果:) 具有一种颜色。

Further, n_return() needs to be a const method for you to be able to invoke it from a const instance. 此外, n_return()必须是const方法 ,您才能从const实例调用它。

This is as closest to your original code that fills the points raised regarding syntax and design, so you could easily isolate the differences. 这与您的原始代码最接近,该原始代码填补了在语法和设计方面提出的要点,因此您可以轻松地区分出差异。 main() stays the same: main()保持不变:

#include <iostream>
#include<stdexcept>

using namespace std;

enum class Color{r,g};

class Apple{
    int n;
    Color c;
public:
    Apple(int n,Color color){
        if(n<0)throw runtime_error("");
        this->n=n;
        this->c=color;
    }
    int n_return() const {return n;}
};
int main(){
    try{
        const Apple a1{10,Color::g};
        cout << a1.n_return();}
    catch(runtime_error&){
        cout<<"ER\n";
    }
    return 0;
}

Note I've changed your enum to enum class . 请注意,我已将您的enum更改为enum class The general reasons for which you can read about here . 您可以在此处阅读的一般原因。

If you want to set the default Color for your Apple upon construction in case it isn't specified, then you can write the declaration for it like this: 如果要在构建时为Apple 设置默认 Color (如果未指定),则可以为它编写声明,如下所示:

// Apple has Color `g` by default
Apple(int n,Color color = Color::g){//...

So you can do this: 因此,您可以执行以下操作:

const Apple a1{10};

and get your Color::g colored apple. 并获取您的Color::g彩色苹果。

As the comments have already pointed out, you're making an (empty) class Color and defining a scoped enum within it. 正如评论中已经指出的那样,您正在创建一个(空)类Color并在其中定义了一个范围内的枚举。 All of this is unnecessary cruft; 所有这些都是不必要的废话。 all you need is the enum. 您需要的只是枚举。 Replace your class Color with 将您的班级Color替换为

enum class Color{r,g};

and don't do : public Color in the Apple declaration. 并且不要这样做: public Color Apple声明中的: public Color

Unrelated, but necessary to get your code working as written: You declare your Apple variable as const but then call a non- const method on it. 不相关,但必须使您的代码正常工作:将Apple变量声明为const ,然后在其上调用非const方法。 In order to make this work, you'll need your n_return to look like this. 为了使此工作有效,您需要n_return看起来像这样。

int n_return() const {return n;}

Notice the const keyword here, to allow the method to be used on const variables. 注意此处的const关键字,以允许在const变量上使用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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