静态索引值的解决方案

发布于 2024-10-18 04:27:40 字数 2276 浏览 1 评论 0原文

请参阅以下代码:

#include <iostream>
#include <string>

using namespace std;

enum dataType {
    DATATYPE_BYTE,
    DATATYPE_CHAR, 
    DATATYPE_UCHAR,
    DATATYPE_SHORT,
    DATATYPE_USHORT,
    DATATYPE_INT,
    DATATYPE_UINT,
    DATATYPE_LONG,
    DATATYPE_ULONG,
    DATATYPE_FLOAT,
    DATATYPE_UFLOAT,
    DATATYPE_DOUBLE,
    DATATYPE_UDOUBLE,

    DATATYPE_BLOB,
    DATATYPE_STRING,
    DATATYPE_COMPLEX, 

    DATATYPE_MORE, 
    DATATYPE_ERROR
};

typedef struct typenamepair_ {
    const dataType name;
    const char* const nameval ;
} typenamepair;

class types {
    private:
        typenamepair *typesArr;
        static typenamepair TYPES[];
        static types* instance_;
    public:
        static types* getInstance() {
            if ( !instance_ )
                instance_ = new types;

            return instance_;
        }
        const char* operator[](dataType typeEnum)
        {
            for ( unsigned int i = 0; i < (sizeof(types::TYPES)/sizeof(typenamepair)); ++i ) {
                if ( i == TYPES[i].name  )
                    return TYPES[i].nameval;
            }
            // failed to get value. return error
            return TYPES[DATATYPE_ERROR].nameval;
        }
};

types* types::instance_ = NULL;
typenamepair types::TYPES[] = {
    { DATATYPE_BYTE, "byte" },
    { DATATYPE_CHAR, "char" },
    { DATATYPE_UCHAR, "u_char" },
    { DATATYPE_SHORT, "short" },
    { DATATYPE_USHORT, "u_short" },
    { DATATYPE_INT, "int" },
    { DATATYPE_UINT, "u_int" },
    { DATATYPE_FLOAT, "float"},
    { DATATYPE_UFLOAT, "u_float"},
    { DATATYPE_DOUBLE, "double"},
    { DATATYPE_UDOUBLE, "u_double"},
    { DATATYPE_STRING, "cstring"},
    { DATATYPE_BLOB, "blob"},
    { DATATYPE_COMPLEX, "complex"},
    { DATATYPE_MORE, "more"},

    // Unknown type!
    { DATATYPE_ERROR, "ERROR"}
};


main()
{
    const char* test = (types::getInstance())[DATATYPE_UINT] ;
    cout << test << endl;
}

给出以下编译错误:

test.cpp:在成员函数 'const char* types::operator' 中: test.cpp:53:错误:“sizeof”对不完整类型“typenamepair []”的无效应用 test.cpp:在函数“int main()”中: test.cpp:87: 错误:无法在初始化中将“类型”转换为“const char*”

的大小有什么问题以及如何修复它? 另外,如何使用返回的实例来使用运算符来获取类型名?

另外,如果有更好的解决方案,请告诉我。 谢谢

Please see the following code:

#include <iostream>
#include <string>

using namespace std;

enum dataType {
    DATATYPE_BYTE,
    DATATYPE_CHAR, 
    DATATYPE_UCHAR,
    DATATYPE_SHORT,
    DATATYPE_USHORT,
    DATATYPE_INT,
    DATATYPE_UINT,
    DATATYPE_LONG,
    DATATYPE_ULONG,
    DATATYPE_FLOAT,
    DATATYPE_UFLOAT,
    DATATYPE_DOUBLE,
    DATATYPE_UDOUBLE,

    DATATYPE_BLOB,
    DATATYPE_STRING,
    DATATYPE_COMPLEX, 

    DATATYPE_MORE, 
    DATATYPE_ERROR
};

typedef struct typenamepair_ {
    const dataType name;
    const char* const nameval ;
} typenamepair;

class types {
    private:
        typenamepair *typesArr;
        static typenamepair TYPES[];
        static types* instance_;
    public:
        static types* getInstance() {
            if ( !instance_ )
                instance_ = new types;

            return instance_;
        }
        const char* operator[](dataType typeEnum)
        {
            for ( unsigned int i = 0; i < (sizeof(types::TYPES)/sizeof(typenamepair)); ++i ) {
                if ( i == TYPES[i].name  )
                    return TYPES[i].nameval;
            }
            // failed to get value. return error
            return TYPES[DATATYPE_ERROR].nameval;
        }
};

types* types::instance_ = NULL;
typenamepair types::TYPES[] = {
    { DATATYPE_BYTE, "byte" },
    { DATATYPE_CHAR, "char" },
    { DATATYPE_UCHAR, "u_char" },
    { DATATYPE_SHORT, "short" },
    { DATATYPE_USHORT, "u_short" },
    { DATATYPE_INT, "int" },
    { DATATYPE_UINT, "u_int" },
    { DATATYPE_FLOAT, "float"},
    { DATATYPE_UFLOAT, "u_float"},
    { DATATYPE_DOUBLE, "double"},
    { DATATYPE_UDOUBLE, "u_double"},
    { DATATYPE_STRING, "cstring"},
    { DATATYPE_BLOB, "blob"},
    { DATATYPE_COMPLEX, "complex"},
    { DATATYPE_MORE, "more"},

    // Unknown type!
    { DATATYPE_ERROR, "ERROR"}
};


main()
{
    const char* test = (types::getInstance())[DATATYPE_UINT] ;
    cout << test << endl;
}

Gives me the following compilation errors:

test.cpp: In member function 'const char* types::operator':
test.cpp:53: error: invalid application of 'sizeof' to incomplete type 'typenamepair []'
test.cpp: In function 'int main()':
test.cpp:87: error: cannot convert 'types' to 'const char*' in initialization

What is wrong with size of and how can I fix it?
Also, how can I use the returned instace to use the operator to get the typename?

Also, please let me know if there is a better solution.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中楼上月下 2024-10-25 04:27:40

对于第一个错误,您需要使用 sizeof 移动代码,直到定义数组内容为止。有一种更好的方法来获取数组大小,但它也不能直到内容被定义为止。

对于第二个错误,您有一个指针,因此找不到用户定义的 operator[] 。更改 getInstance 以返回引用而不是指针,它应该可以工作。

For your first error, you need to move the code using sizeof until after the array contents have been defined. There is a better way to get the array size but it also can't be used until the contents are defined.

For your second error, you have a pointer, so your user-defined operator[] isn't being found. Change getInstance to return a reference instead of a pointer and it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文