Invalid use of non static data member : Working With 2-D Char Arrays

I have to create a program to store information for a book store using class.
I need to store names of 10 books,along with authors,publishers,cost,stock,quality etc....The Thing is I have to create a 2-D Array to to store book names,publishers literally everything and i have been getting this error while using 2-D array to store book names.
1. Invalid Use Of Non-Static DATA Member.
2. bookarray Was Not Declared in this scope.
3. Line 32 : cannot resolved address of overloaded function.

Actually there seems to be no problem if i do it without classes.

I could use dynamic allocation via pointers but question as strictly demanded a 2-d array.

This is my first time working with classes (it's only been 3 days,sorry if this is super dumb or just horrible coding) I have removed the unnecessary code.

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
29
30
31
32
33
34
35
#include<iostream>
using namespace std;
class STOCK
{
private :

int book_numbers ;
int book_name_size ;
public :

    STOCK()
    {
        book_numbers = 10 ;
        book_name_size = 50;
    }

    char bookarray[book_numbers][book_name_size];
    int i ;
    void readbooksnames()
    {
         for (i = 0 ; i < book_numbers ; ++i)
         {
             cin.getline(bookarray[i],book_name_size);
         }
    }
};
int main()
{
    STOCK obj ;
    obj.readbooksnames;
    return 0 ;
}


Last edited on
The bounds of an array should be constants.
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
29
30
31
32
33
#include<iostream>
using namespace std;

class STOCK
{
private:

    static const int book_numbers = 10;
    static const int book_name_size = 50;
    char bookarray[book_numbers][book_name_size];

public:

    STOCK()
    {
    }

    void readbooksnames()
    {
         int i; // local variables belong in the function
         for (i = 0; i < book_numbers; ++i)
         {
             cin.getline(bookarray[i],book_name_size);
         }
    }
};

int main()
{
    STOCK obj;
    obj.readbooksnames();  // function calls need ()
    return 0;
}

Hi tpb

question,

why make the members static const instead of just const?

If you have multiple objects this can be an advantage as the compiler doesn't have to make separate members in this case book_numbers and book_name_size right?

thanks
@adam2016, It gives me a "invalid use of non-static data member" error on the line that defines bookarray if they aren't static.
Topic archived. No new replies allowed.