静态索引值的解决方案
请参阅以下代码:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于第一个错误,您需要使用
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. ChangegetInstance
to return a reference instead of a pointer and it should work.