尝试使用类方法打印对象内容。继续获取“错误:语句无法解析过载功能的地址”。

发布于 2025-01-22 08:47:02 字数 1282 浏览 3 评论 0原文

初学者在这里,我正在为课程进行作业,在研究该计划时,我遇到了一些麻烦。我无法弄清楚如何使用与我制作的类不同的方法来打印我在主要方法中拥有的对象中的内容。

这是我的代码:

#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;
}

我需要使用 b1 b2 b2 的内容使用 getbookinfo()方法,但是每次我认为我知道我在做什么,我会在 b1.getbookinfo b2.getBookinfo

。它是我自己的,因此输出并没有全部束缚在一起,但我什至无法让它输出任何东西!

这是我第一次与课堂和构造师一起工作,所以我现在真的迷失了方向。帮助您表示赞赏!谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一曲爱恨情仇 2025-01-29 08:47:02

错误来自这里:

b1.getBookInfo;
b2.getBookInfo;

您正在尝试调用类的方法,但没有使用呼叫操作员(),因此您正在加载方法的地址。要解决此错误,请使用呼叫操作员来调用方法:

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

除了您的代码有很多错误,但您表示您是初学者,所以这并不罕见

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文