-
February 11th, 2019, 01:49 PM
#1
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
-
February 11th, 2019, 01:59 PM
#2
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
-
February 11th, 2019, 02:00 PM
#3
Re: error: invalid types ‘unsigned int[unsigned int]’ for array subscript
Will do! thanks for the help
-
February 11th, 2019, 02:06 PM
#4
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
-
February 12th, 2019, 12:14 AM
#5
Re: error: invalid types ‘unsigned int[unsigned int]’ for array subscript
Originally Posted by thishas
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|