Home > database >  Trying to print out object contents using a class method. Keep getting "error: statement cannot
Trying to print out object contents using a class method. Keep getting "error: statement cannot

Time:04-19

beginner here, I am working on an assignment for a course and while working on this program, I am experiencing some troubles. I cannot figure out how to print out the contents within an object I have in my main method using a different method from a class I made.

Here's my code:

#include <iostream>
using namespace std;

class Book {
    private:
        string title;
        int pages;
        double price;
    public:
        Book () {
            title = "";
            pages = 0;
            price = 0.0;
        }
        Book(string t, int p, double pr) {
            title = t;
            pages = p;
            price = pr;
        }
        void setPrice(double);
        void getBookInfo(Book, Book);
};

void Book::setPrice(double pr) {
    price = pr;
}

void Book::getBookInfo(Book b1, Book b2) {
    cout << b1.title << b1.pages << b1.price << endl;
    cout << b2.title << b2.pages << b2.price << endl;
}

int main() {

    Book b1("C   Programming", 802, 90.55);
    Book b2("Chemistry Tests", 303, 61.23);

    b2.setPrice(77.22);

    b1.getBookInfo;
    b2.getBookInfo;

    return 0;
}

I need to print out the contents of Book b1 and Book b2 using the getBookInfo() method, but every time I think I know what I'm doing, I get "error: statement cannot resolve address of overloaded function" on b1.getBookInfo and b2.getBookInfo.

Of course, I will format it on my own so the output isn't all bunched together, but I can't even get it to output anything!

This is my first time working with class and constructors, so I am really kinda lost right now. Help is appreciated! Thank you!

CodePudding user response:

The error is coming from here:

b1.getBookInfo;
b2.getBookInfo;

you are trying to call methods of a class but didn't use the call operator () so you are instead loading the address of the method. To solve this error use the call operator to call the methods:

b1.getBookInfo(b1, b2);
b2.getBookInfo(b1, b2);

beside that your code has many faults but you indicated you are a beginner so this isn't unusual

  • Related