dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: error: invalid types ‘unsigned int[unsigned int]’ for array subscript

  1. #1
    Join Date
    Feb 2019
    Posts
    2

    error: invalid types ‘unsigned int[unsigned int]’ for array subscript

    Why am I getting invalid type on

    Project1.cpp:139:32: error: invalid types ‘unsigned int[unsigned int]’ for array subscript
    os << Mon[month(date)-1];

    Code:
    void displayDate(const Date& date, ostream& os, DATE_STYLE ds)
    {
    
     // if ds == MM_DD_YYYY)
    
      if (!wellFormed(date))
      {
         cout << "Date Error\n";
         exit(1);
      }
      else
        if (ds == MM_DD_YYYY)
        {
          if (numDigits(date) == 7)
            os << '0';
            os << month(date) << '/';
            if (nthDigit(date, 5) == 0)
               os << '0';
               os << day(date) << '/';
               unsigned y = year(date);
               unsigned len = 4 - numDigits(y);
               for (unsigned i = 0; i < len; ++i)
                 os << '0';
                 os << year(date) << endl;
         }
         else
           string Mon[12] = {"Jan ", "Feb ", "Mar ", "Apr ", "May ", "June ", "Jul ", "Aug ",
           "Sept ", "Oct ", "Nov ", "Dec " };
            unsigned Mon;
            os << Mon[month(date)-1];
            if (nthDigit(date, 5) == 0)
            os << '0';
            os << day(date) << ",";
            unsigned y = year(date);
            unsigned len = 4 - numDigits(y);
            for (unsigned i = 0; i < len; ++i)
            {
              os << '0';
              os << year(date) << endl;
            }
    }
    Last edited by VictorN; February 11th, 2019 at 01:56 PM. Reason: Added code tags

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,448

    Re: error: invalid types ‘unsigned int[unsigned int]’ for array subscript

    Please, always add CODE tags around code snippets!
    Read the Announcement: Before you post....
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2019
    Posts
    2

    Re: error: invalid types ‘unsigned int[unsigned int]’ for array subscript

    Will do! thanks for the help

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,448

    Re: error: invalid types ‘unsigned int[unsigned int]’ for array subscript

    Well, now look at the formatted code snippet...
    this part:
    Code:
    else
        if (ds == MM_DD_YYYY)
        {
          if (numDigits(date) == 7)
            os << '0';
            os << month(date) << '/';
            if (nthDigit(date, 5) == 0)
               os << '0';
               os << day(date) << '/';
               unsigned y = year(date);
               unsigned len = 4 - numDigits(y);
               for (unsigned i = 0; i < len; ++i)
                 os << '0';
                 os << year(date) << endl;
         }
         else
           string Mon[12] = {"Jan ", "Feb ", "Mar ", "Apr ", "May ", "June ", "Jul ", "Aug ",
           "Sept ", "Oct ", "Nov ", "Dec " };
            unsigned Mon;
    is the same as if you would write it as
    Code:
    else if (ds == MM_DD_YYYY)
    {
          if (numDigits(date) == 7)
          {  
                os << '0';
          }
            os << month(date) << '/';
            if (nthDigit(date, 5) == 0)
            {
                  os << '0';
            }
               os << day(date) << '/';
               unsigned y = year(date);
               unsigned len = 4 - numDigits(y);
               for (unsigned i = 0; i < len; ++i)
              {
                os << '0';
               }
    
                 os << year(date) << endl;
         }
         else
         {
           string Mon[12] = {"Jan ", "Feb ", "Mar ", "Apr ", "May ", "June ", "Jul ", "Aug ",
           "Sept ", "Oct ", "Nov ", "Dec " };
         }
            unsigned Mon;
            ...
    Is it the same as you have expected?
    Victor Nijegorodov

  5. #5
    Join Date
    Feb 2017
    Posts
    465

    Re: error: invalid types ‘unsigned int[unsigned int]’ for array subscript

    Quote Originally Posted by thishas View Post
    Why am I getting invalid type on
    This seems bogus to me,

    Code:
            unsigned Mon;
            os << Mon[month(date)-1];
    You are declaring Mon to be an ordinary unsigned int but then you use it as if it were an array.

    The other Mon,

    Code:
    string Mon[12] = .......;
    is out of scope as VictorN suggests.
    Last edited by wolle; February 12th, 2019 at 12:23 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)