Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to print out object contents using a class method. Keep getting "error: statement cannot resolve address of overloaded function"

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading