繁体   English   中英

通过函数返回枚举

[英]Return enum through a function

我试过研究这个,但可以找到适合我情况的解决方案。

所以我正在尝试为mysql字段的元数据创建一个基本包装器:

#pragma once

#include <string>
#include <stdint.h>
#include <windows.h>
#include "mysql.h"


namespace escobar { namespace storage {

    class mysql_field_metadata
    {
    private:
        uint32_t result_index;          /* For indexing the field for a mysql record. */
        std::string name;               /* The name of the column. */
        std::string original_name;      /* The original name of the column, if the name is an alias. */
        std::string table;              /* Name of the table */
        std::string original_table;     /* The original name of the table, if the table name is an alias. */
        std::string database;           /* The name of the database the table/record belongs to. */
        uint32_t length;                /* The length of the field. */
        uint32_t max_length;            /* The maximum length of the set. */
        uint32_t decimals;              /* Number of decimals used in the field. */
        uint32_t charset;               /* Table charset. */
        enum enum_field_types type;     /* The type of MYSQL data. */

    public:
        mysql_field_metadata(MYSQL_FIELD* field_data, uint32_t _result_index) : 
            result_index(_result_index), name(field_data->name),
            original_name(field_data->org_name), table(field_data->table),
            original_table(field_data->org_table), database(field_data->db),
            length(field_data->length), max_length(field_data->max_length),
            decimals(field_data->decimals), charset(field_data->charsetnr),
            type(field_data->type)
        {
        }

        ~mysql_field_metadata(void) { }
        std::string get_name(void) { return this->name; }
        std::string get_original_name(void) { return this->original_name; }
        std::string get_table(void) { return this->original_name; }
        std::string get_original_table(void) { return this->original_table; }
        std::string get_database(void) { return this->database; }
        uint32_t get_length(void) { return this->length; }
        uint32_t get_max_length(void) { return this->max_length; }
        uint32_t get_decimals(void) { return this->decimals; }
        uint32_t get_charset(void) { return this->charset; }
        enum emum_field_types get_type(void) { return this->type; }
    };

}}

如你所见,我的最后一个功能:

enum emum_field_types get_type(void) { return this->type; }

它似乎没有工作,我得到以下错误:

error C2440: 'return' : cannot convert from 'enum_field_types' to 'escobar::storage::emum_field_types' 1> Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)

不需要用前缀它enum 您可以尝试从功能和成员中删除它并报告回来。 这可能会让编译器感到困惑。

enum_field_types type;     /* The type of MYSQL data. */
...
emum_field_types get_type(void) {

编辑:要修复“unntifier is unclared”问题,请使用namespace::typename语法(如果它在命名空间中)或::typename如果没有命名空间)

从编译器错误来看,似乎enum_field_types有两种不同的声明。 可能一个在你的命名空间里面,一个是全局的。 您有两种可能的解决方案:

  1. 如果两个声明都在您自己的代码中,您可以跟踪这两个声明并删除其中一个声明。

  2. 您可以使用namespace::typename语法限定要使用的声明。

选择1.应该是首选,因为对两个不同的声明使用相同的名称,特别是当其中一个声明在全局命名空间中时,可能会导致一些细微的错误和编译器错误。

暂无
暂无

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

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