Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3968806
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:53:14+00:00 2026-05-20T03:53:14+00:00

Please see the following code: #include <iostream> #include <string> using namespace std; enum dataType

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:53:14+00:00Added an answer on May 20, 2026 at 3:53 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.