1

I have created a class Book that store 3 variables(string title, int pages, float price). I also have created 3 constructor but the null constructor keeps giving me errors. (58: error: request for member 'pages' in 'b1', which is of non-class type 'Book()') (61: error: request for member 'Info' in 'b1', which is of non-class type 'Book()')

#include <iostream>
#include <string>

class Book{
    public:

    std::string title;
    int pages;

    private:

    float price;

    public:

    Book(){

        title = "";
        pages = 0;
        price = 0;
    }

    Book(std::string bookTitle){

        title = bookTitle;
        pages = 0;
        price = 0;
    }

    Book(std::string bookTitle, float bookPrice){

        title = bookTitle;
        pages = 0;
        price = bookPrice;
    }

    void Info()
    {
        std::cout << "Title: " << title << std::endl;
        std::cout << "Pages: " << pages << std::endl;
        std::cout << "Price: " << price << std::endl;
    }

    void Pricing(float bookPrice)
    {
        price = bookPrice;
    }

    };

int main()
{
    Book b1();
    b1.pages = 100; //first error

    Book b2("Mathimatics");
    b2.pages = 200;

    Book b3("Algebra", 50);
    b3.pages = 300;

    b1.Info(); //second error
    b2.Info();
    b3.Info();

}
  • 3
    Does this answer your question? [Default constructor with empty brackets](https://stackoverflow.com/questions/180172/default-constructor-with-empty-brackets) – Algirdas Preidžius Jun 03 '20 at 10:00

1 Answers1

4
Book b1();

This is a declaration of a function called b1 with no parameters and Book return type. Simply change it to

Book b1;

or

Book b1{};

or

auto b1 = Book();

etc.

Daniel Langr
  • 22,196
  • 3
  • 50
  • 93