You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++新手求助:使用类方法打印对象内容时遭遇“无法解析重载函数地址”错误

C++: Error when calling class method to print object info - cannot resolve address of overloaded function

I'm a programming newbie working on my course assignment, and I'm stuck on an issue. I can't print the contents of objects created in main() using a method in my custom class. 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 use the getBookInfo() method to print the contents of Book objects b1 and b2, but every time I try, I get the error: "error: statement cannot resolve address of overloaded function" at b1.getBookInfo and b2.getBookInfo. I'll adjust the output format later, but right now I can't even get basic output working. This is my first time using classes and constructors, so I'm really confused. Any help would be appreciated, thanks!


Let's break down what's going wrong here and fix it step by step:

1. The immediate syntax error

The error you're seeing is because you're not calling the method correctly. When you write b1.getBookInfo, you're just referencing the address of the function, not actually invoking it. To call a function, you need to add parentheses () — and since your getBookInfo expects two Book parameters, you'd technically need to pass b1 and b2 like this:

b1.getBookInfo(b1, b2);

But this is a weird design for a member function — there's no reason to pass the objects to a method that already belongs to the Book class. Let's fix the bigger design issue instead.

2. Fixing the getBookInfo method design

Member functions of a class can directly access the private members of the current object (using this implicitly), so getBookInfo should be designed to print the information of the object it's called on, not two separate objects. Here's how to adjust it:

Step 1: Update the class declaration

Change the getBookInfo prototype to take no parameters:

void getBookInfo();

Step 2: Update the method implementation

Modify the function to print the current object's data:

void Book::getBookInfo() {
    cout << "Title: " << title << ", Pages: " << pages << ", Price: " << price << endl;
}

Step 3: Call the method correctly in main

Now you can call getBookInfo() on each object to print its own details:

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

3. Full corrected code

Here's the complete fixed version of your code:

#include <iostream>
#include <string> // Don't forget this! You need it to use std::string
using namespace std;

class Book {
private:
    string title;
    int pages;
    double price;
public:
    // Default constructor (using cleaner initializer list)
    Book () : title(""), pages(0), price(0.0) {}
    
    // Parameterized constructor (using initializer list)
    Book(string t, int p, double pr) : title(t), pages(p), price(pr) {}
    
    void setPrice(double);
    void getBookInfo();
};

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

void Book::getBookInfo() {
    cout << "Title: " << title << ", Pages: " << pages << ", Price: " << 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;
}

Bonus notes for your learning

  • I added #include <string> — your code might have compiled without it in some compilers because iostream sometimes includes it indirectly, but it's good practice to include it explicitly when using std::string.
  • I used initializer lists in the constructors (: title(""), pages(0), price(0.0)). This is a cleaner and more efficient way to initialize class members than assigning them inside the constructor body.
  • If you ever need a function to print multiple Book objects at once, consider making it a static member function or a regular global function instead of a non-static member function, since it wouldn't belong to a single Book instance.

内容的提问来源于stack exchange,提问作者krasi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 18:52:32