String array question
Dec 18, 2015 at 11:35am UTC
Why is this a problem?
1 2 3 4 5 6
std::string sBannerLine1[11]
sBannerLine1[11]=
{
"_" , "_" , "_" , "_" , "_" , "_" , "_" , "_" , "_" , "_" , "_"
};
Last edited on Dec 18, 2015 at 11:37am UTC
Dec 18, 2015 at 11:56am UTC
sBannerLine1[11] is accessing the 12:th element in the array. The array only has 11 elements so that is a problem.
Even if you remove [11] it would still not work because arrays can unfortunately not be assigned. If you want to assign like this you could use a std::array instead.
1 2 3 4 5 6 7 8 9 10 11 12
#include <array>
#include <string>
int main()
{
std::array<std::string, 11> sBannerLine1;
sBannerLine1 =
{
"_" , "_" , "_" , "_" , "_" , "_" , "_" , "_" , "_" , "_" , "_"
};
}
Last edited on Dec 18, 2015 at 11:57am UTC
Dec 18, 2015 at 12:19pm UTC
Given that you left out the semi-colon in line 1, I think you were trying to do:
1 2 3 4
std::string sBannerLine1[] =
{
"_" , "_" , "_" , "_" , "_" , "_" , "_" , "_" , "_" , "_" , "_"
};
However, if this isn't what you intended, @Peter87 has given one of the proper ways of going about "assigning to arrays".
Topic archived. No new replies allowed.