Using begin(string[]) in a class

Using begin(string[]) in a class results in a compilation error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class S
{
    string season;

    static const string seasons[];

 public:
     S()
     {
     }

     void seek(string& s)
     {
	/// compilation-error
        const string* p = find(begin(seasons), end(seasons), s);

        if (p == end(seasons))
            cout << s << " not found" << endl;
        else
            cout << s << " found" << endl;
     }
};


const string S::seasons[]
   {
       "spring", "summer", "fall", "winter"
   };



Compilation-error is:

In member function 'void S::seek(std::string&)':|
error: no matching function for call to 'begin(const string [])'|
Note: candidates are:
note: template<class _Tp> constexpr const _Tp* std::begin(std::initializer_list<_Tp>)|
etc....


What is the solution?


The error doesn't occur outside a class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const string seasons[]
   {
       "spring", "summer", "fall", "winter"
   };


void test(const string& s)
{
    /// OK
    const string* p = find(begin(seasons), end(seasons), s);

    if (p == end(seasons))
       cout << s << " not found" << endl;
    else
       cout << s << " found" << endl;
}


Thanks
Last edited on
On line 15, the declared type of the array seasons[] is an array of unknown bound;
it is an incomplete type at that point in the translation unit.


> What is the solution?

Declare it as a complete type (as an array of known bound). For instance:

1
2
3
4
5
6
7
class S
{
    std::string season;

    static constexpr std::size_t N = 4 ;
    static const std::string seasons[N]; // declaration of an array of known bound
    // ... 

Topic archived. No new replies allowed.