Trouble with Subclasses: invalid use of nonstatic member func

Okay, so I'm writing a code for an assignment that is meant to demonstrate classes, subclasses and inheritance. The idea being there is a superclass called Book and the objects in Book can be added to the subclass Shelf. Where Shelf can hold a maximum of 10 objects.

I think I've got everything in working order, but when I try to add an object to the subclass I get the following error message:

error: invalid use of non-static member function
shelf::addBooks(books[i].getName, i, addNum);
^

The carrot leads me to believe that its not the arguments that are wrong but the syntax I've used since it's pointing to the close bracket.


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  // classes defined here
    #include <iostream>
    #include <string>

    class book
    {
    public:
        //constructor
        book(std::string Name, int pNum);


        std::string getName(); //take in mem name
        int getPageNum(); //take in mem pg numbers


        void setPageNum(int pNum); //set


        std::string m_Name;
        int m_pNum;


        ~book();

    };

    class shelf : public book
    //shelf inherits from class book
    {
    public:
        shelf(std::string Name, int pNum);

        /*
        NOTE: std::string getName() and getPageNum() do not need to be included
        in this definition as they are inherited behaviours 
        */

        //these are the new behaviours unique to shelf
        std::string addBooks(std::string Name, int currentHeld, int BooksAdded);
        //added as a string since the only output will be "book added to shelf"
        //name of book added to shelf, number of books currently in shelf, total books to be added to shelf

        ~shelf();


        std::string m_Name;
        int m_Num;
    };

//class functions defined here

    book::book(std::string Name, int pNum)
        : m_Name(Name),m_pNum(pNum)
    {

    }

    book::~book()
    {
        //burn the books
    }

    void book::setPageNum(int NewpNum)
    {
        this->m_pNum = NewpNum;
    }

    int book::getPageNum()
    {
        return m_pNum;
    }

    std::string book::getName()
    {
        return m_Name;
    }

    shelf::shelf(std::string Name, int pNum)
    : book(m_Name, m_Num)
    {

    }

    shelf::~shelf()
    {
    //destructor
    }
    
      std::string shelf::addBooks(std::string Name, int currentHeld, int BooksAdded)
    {
    for(int i = 0; i < BooksAdded; i++)
    {
        currentHeld = currentHeld + 1;
        if (currentHeld <= 10)
        {
        std::cout << book::getName() << " has been added to the self" << std::endl;
        }
        else
        {
        std::cout << "Well done. The shelf is too small and the books all fell!" << std::endl;
        }
    }
    }


//main body

    int main()
    {
        //declare all books in an array
        book books[11] =
        {
            book("1984", 84),
            book("Animal Farm", 190),
            book("Harry Potter", 8),
            book("LOTR", 300),
            book("Flypaper", 40),
            book("Great Expectations", 1000),
            book("Wuthering Heights", 562),
            book("Slaughterhouse 5", 5),
            book("Mrs Dalloway",5),
            book("The Mister Men", 5),
            book("C++ for Dummies", 180)
        };

        int i = 0; //loop counter
    int addNum = 11; //number of books to add to shelf

        for (i = 0; i < 11; i++)
        {
            shelf::addBooks(books[i].getName, i, addNum);
        }

        return 0;
    }


NOTE: I know convention tells me I should be using a header and two cpp files BUT I've written it all in this big ugly block because of how my uni structures their websubmission system
Last edited on
You cannot call non static member functions like this
shelf::addBooks(books[i].getName, i, addNum);

You need to create an object.
1
2
shelf foo("Name", 1);
foo.addBooks(books[i].getName, i, addNum);
ahh okay that makes sense. Thanks!

In terms of memory allocation does that mean that there is memory allocated for objects in books and a separate memory allocated for objects in shelf too?
Topic archived. No new replies allowed.