如何用c ++中的枚举参数实例化一个对象?

时间:2017-10-17 10:21:28

标签: c++ visual-studio-2010 oop

我试图用3个参数实例化一个对象'Bug bug',其中一个是枚举器。这是我的班级:

 class Bug 
{
private:
    int Id;
    string description;
    enum severity { low, medium, severe} s;

public:
    Bug(void);
     Bug(int id, string descr, severity x)
              :Id(id), description(descr), s(x)
     {}

    void printDetails()
    {
       cout<< "Severity level:" <<s<< " Description: " <<description<<" ID= " 
       <<Id<< endl;
    }
   ~Bug(void);

};

这是我的main.cpp:

    #include "Bug.h"
    int main(){

    Bug bg(3,"a", low);//Error message: identifier "low" is undefined

     return 0;
    }

当我将此行添加到主

    enum severity { low, medium, severe};

错误消息已更改为:

      Bug bg(3,"a", low);//Error message: no instance of constructor "Bug::bug" matches the argument list

任何想法如何做到这一点?

3 个答案:

答案 0 :(得分:4)

你的枚举存在于Bug类中,而你的主要功能在类外。这是必须的。

因此,从主函数引用枚举的正确方法是:

Bug bg(3,"a", Bug::low);

但是,您需要在类的public部分中定义枚举。它目前位于private部分内,这将阻止主要功能访问它。

请注意,您还需要将枚举定义为类中的类型,与使用它的私有成员变量分开。所以你的课程定义应该是这样的:

class Bug 
{
public:
    typedef enum {low, medium, severe} severity;
    Bug(void);
     Bug(int id, string descr, severity x)
              :Id(id), description(descr), s(x)
     {}

    void printDetails()
    {
       cout<< "Severity level:" <<s<< " Description: " <<description<<" ID= " 
       <<Id<< endl;
    }
   ~Bug(void);

private:
    int Id;
    string description;
    severity s;
};

请注意,public部分必须高于此类中的private部分,以便在使用之前定义枚举类型严重性。

答案 1 :(得分:0)

将枚举移至公共区域并尝试使用:

CREATE OR REPLACE FUNCTION cdi.automating_partitions()
RETURNS TABLE(natural_id text, name text, natural_id_numeric text) AS
$func$
DECLARE
   formal_table text;
BEGIN
   FOR formal_table IN
       select '2017-01-01'::date + (n || ' months')::interval months,
       '2013-02-01'::date + (n || ' months')::interval monthsplus
       from generate_series(0, 12) n
   LOOP
      RETURN QUERY EXECUTE
   'CREATE TABLE cdi.' || 'document' || to_char(months, 'YYYY')  || ''  || to_char(months, 'MM') || ' PARTITION OF cdi.document
 FOR VALUES FROM  (''' ||  to_char(months, 'YYYY')  || to_char(months, 'MM')  || ''',
''' to_char(monthsplus, 'YYYY')  || to_char(monthsplus, 'MM')   ''');'
   END LOOP;
END
$func$  LANGUAGE plpgsql;

答案 2 :(得分:0)

修复如下,请参阅我的评论

class Bug 
{


public:
    // If you intent to use severity outside class , better make it public
    enum severity { low, medium, severe} ;

    Bug(void);
     Bug(int id, string descr, severity x)
              :Id(id), description(descr), s(x)
     {}

    void printDetails()
    {
       cout<< "Severity level:" <<s<< " Description: " <<description<<" ID= " 
       <<Id<< endl;
    }

   ~Bug()
   {
       // Fix your constructor, they don't have void parameter and must have a body
   }

   // Moving private section, just to use the severity declaration in public section
private:
    int Id;
    string description;
    severity s ;    // use an instance of severity for internal use
};

int main()
{
    Bug bg(3,"a", Bug::low); // now use severity values using Bug::


}